I working on the "next steps" to Laravel 8 From Scratch- specifically step 2)Update the "Edit Post" page in the admin section to allow for changing the author of a post.
I am trying to better understand why I'm not getting any feedback on the page with dd() after I call the validatePost() method within the update() method in AdminPostController.php. I think I'm missing an understanding of the validatePost() method that is called within update(), but I don't know.
I saw there was an issue with validations on large requests failing silently, but the solution for those was to change SESSION_DRIVER in .env to file from cookie- however, mine is already set to file. Also in session.php I have 'driver' => env('SESSION_DRIVER', 'file'), for what it's worth.
Here's the code within AdminPostController.php:
public function update(Post $post)
{
// This works- data from $post is displayed when I click on update on the form
// dd($post);
$attributes = $this->validatePost($post);
// These don't work
// dd($attributes);
// dd('test');
if ($attributes['thumbnail'] ?? false) {
$attributes['thumbnail'] = request()->file('thumbnail')->store('thumbnails');
}
if (is_null($post->published_at) && $attributes['is_published'] == 1) {
$attributes['published_at'] = now();
}
$post->update($attributes);
return back()->with('success', 'Post Updated!');
}
protected function validatePost(?Post $post = null): array
{
$post ??= new Post();
return request()->validate([
'title' => 'required',
'thumbnail' => $post->exists ? ['image'] : ['required', 'image'],
'slug' => ['required', Rule::unique('posts', 'slug')->ignore($post)],
'excerpt' => 'required',
'body' => 'required',
'category_id' => ['required', Rule::exists('categories', 'id')],
'is_published' => 'required'
]);
}
And here is the code for the form in edit.blade.php (I'm not sure how to get the syntax highlighting to work, sorry):
<x-layout>
<x-setting :heading="'Edit Post: ' . $post->title">
<form method="POST" action="/admin/posts/{{ $post->id }}" enctype="multipart/form-data"/>
@csrf
@method('PATCH')
<x-form.input name="title" :value="old('title', $post->title)" />
<x-form.input name="slug" :value="old('slug', $post->slug)"/>
<div class="flex mt-6">
<div class="flex-1">
<x-form.input name="thumbnail" type="file" :value="old('thumbnail', $post->thumbnail)"/>
</div>
<img src="{{ asset('storage/' . $post->thumbnail) }}" alt="" class="rounded-xl ml-6" width="100">
</div>
<x-form.textarea name="excerpt">{{ old('excerpt', $post->excerpt) }}</x-form.textarea>
<x-form.textarea name="body">{{ old('body', $post->body) }}</x-form.textarea>
<x-form.field>
<x-form.label name="category" />
<select name="category_id" id="category_id">
@foreach (\App\Models\Category::all() as $category)
<option
value="{{ $category->id }}"
{{ old('category_id', $post->category_id) == $category->id ? 'selected' : '' }}
>{{ ucwords($category->name) }}</option>
@endforeach
</select>
<x-form.error name="category" />
</x-form.field>
@if ((bool)!$post->is_published)
<x-form.field>
<x-form.label name="Do you want to publish your post?" />
<select name="is_published" id="is_published">
<option value=1 >Yes</option>
<option value=0 selected>No, keep it as a draft</option>
</select>
</x-form.field>
@endif
<x-form.button>Update</x-form.button>
</form>
</x-setting>
</x-layout>