I am creating a simple blog site with CRUD functionality in Laravel 8. I have done this before using the deprecated Laravel forms, but am now trying to do it using HTML forms.
However, I am having some problem with the update part of the website. I have a controller called BlogsController with this function to be called to update the blog in the database which should be called when the user submits the form to update.
BlogsController update function
public function update(Request $request, $id) { $this->validate($request, [ 'title' => 'required', 'body' => 'required', 'category' => 'required', 'description' => 'required', 'read_time' => 'required' ]); $blog = Blog::find($id); $blog->title = $request->input('title'); $blog->body = $request->input('body'); $blog->category = $request->input('category'); $blog->description = $request->input('description'); $blog->read_time = $request->input('read_time'); $blog->user_id = auth()->user()->id; $blog->save(); return redirect('/dashboard')->with('success', 'Blog Updated'); }
What action does the form need to direct to?
Top of update form
<form method="POST" action="update">
Route in web.php
Route::resource('blog', 'AppHttpControllersBlogsController');
Implementation in Laravel forms
{!! Form::open(['action' => ['AppHttpControllersBlogsController@update', $blog->id], 'method' => 'POST']) !!}
Advertisement
Answer
You can get a list of all your application routes and names by running
$ php artisan route:list
For your blog update route you should use
<form method="POST" action="{{ route('blog.update', ['blog' => $blog]) }}"> @method('PATCH') </form>
in your template.
Make sure you have you have your csrf
token set correctly at your form by using the @csrf
, see Laravel docs.