简体   繁体   中英

Laravel 8 form to update database

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', 'App\Http\Controllers\BlogsController');

Implementation in Laravel forms

{!! Form::open(['action' => ['App\Http\Controllers\BlogsController@update', $blog->id], 'method' => 'POST']) !!}

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.

I assume you are using Resource route for your CRUD functionality. In that case, Update method in resource controller called via PUT method and not POST. So, in your form, just add this to change the Form submission method to PUT:

<input name="_method" type="hidden" value="PUT">

Also, in your form declaration, you have to add the destination route like this:

<form method="POST" action="{{ route('blog.update', $blog->id) }}">

One thing that's cool with Laravel is Route model binding . So what's that mean? We can do something like this in your update method

// BlogController.php

public function update(Request $request, Blog $blog) {
    $request->validate([
        'title' => 'required',
        'body' => 'required',
        'category' => 'required',
        'description' => 'required',    
        'read_time' => 'required' 
    ]);

    $blog->title = $request->title;
    $blog->body = $request->body;
    $blog->category = $request->category;
    $blog->description = $request->description;
    $blog->read_time = $request->read_time;

    if ($blog->save()) {
        return redirect('/dashboard')->with('success', 'Blog Updated');
    } else {
        // handle error.
    }
}

In your template, you'll want to make sure you're using a PATCH method:

<form method="POST" action="{{ route('blog.update', ['blog' => $blog]) }}">
    @csrf
    @method('PATCH')
    ...
</form>

You also have the option of using the action to get a URL to the registered route:

action('App\Http\Controllers\BlogsController@update', ['blog' => $blog])

Check all route list:

        $ php artisan route:list

your route should be like:

        <form method="POST" action="{{ route('blog.update', ['blog' => $blog]) }}">              
           {{csrf_field()}}
           {{ method_field('PATCH') }}
        </form> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM