简体   繁体   English

Laravel 8表格更新数据库

[英]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. 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 的 controller 和这个 function 被调用来更新数据库中的博客,当用户提交要更新的表单时应该调用它。

BlogsController update function博客控制器更新 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 web.php 中的路线

Route::resource('blog', 'App\Http\Controllers\BlogsController');

Implementation in Laravel forms 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.确保使用@csrf在表单中正确设置了csrf令牌,请参阅 Laravel 文档。

I assume you are using Resource route for your CRUD functionality.我假设您正在为您的 CRUD 功能使用资源路由。 In that case, Update method in resource controller called via PUT method and not POST.在这种情况下,通过 PUT 方法而不是 POST 调用资源 controller 中的更新方法。 So, in your form, just add this to change the Form submission method to PUT:因此,在您的表单中,只需添加以下内容即可将表单提交方法更改为 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 . Laravel 很酷的一件事是Route model binding So what's that mean?那是什么意思? We can do something like this in your update method我们可以在你的update方法中做这样的事情

// 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:在您的模板中,您需要确保使用的是PATCH方法:

<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将 URL 获取到已注册的路由:

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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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