未找到 Laravel 列:“字段列表”中的 1054 列“_token”未知(SQL:更新“教程”设置“_token”=

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30749608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:45:15  来源:igfitidea点击:

Laravel Column not found: 1054 Unknown column '_token' in 'field list' (SQL: update `tutorials` set `_token` =

phplaravel

提问by user3633186

If i update my Form Model Binding i am getting the above error. I have already put all my database fields to the fillable field with no affect i am still getting the error.

如果我更新我的表单模型绑定,我会收到上述错误。我已经把我所有的数据库字段都放到了可填写的字段中,但没有任何影响,但我仍然收到错误消息。

This is the Form of my edit view

这是我的编辑视图的形式

{!! Form::model($tutorial, ['route' => ['tutorials.update', $tutorial->id], 'method' => 'PUT' ]) !!}

My fillable / guarded fields in my Model

我的模型中的可填写/受保护的字段

 /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable =
    [
        'title',
        'subtitle',
        'content',
        'meta_desc',
        'meta_title',
        'seo_title',
        'tags',
        'slug',
        'updated_at',
        'added_on'
    ];

    protected $guarded = ['id', '_token'];

This is my TutorialController

这是我的 TutorialController

public function update(Tutorial $tutorial)
    {

        $input = Input::except('_method'); // Request::all() is not working
        $tutorial->update($input);

        return Redirect::route('tutorials.index')->withSuccess(
            'success.'
        );
    }

And last but not least my routes

最后但并非最不重要的是我的路线

 Route::get('edit/{id}', [
        'as' => 'tutorials.edit',
        'uses' => 'TutorialsController@edit'
    ]);

    Route::put('edit/{id}', [
        'as' => 'tutorials.update',
        'uses' => 'TutorialsController@update'
    ]);

回答by Joel Hinz

Laravel adds a CSRF protection token called _tokento your form. You need to remove that from the input as well, otherwise Laravel thinks it is a part of what you want to insert.

Laravel_token向您的表单添加了一个 CSRF 保护令牌。您还需要从输入中删除它,否则 Laravel 认为它是您要插入的内容的一部分。

$input = Input::except('_method', '_token');

Then, you should remove the $guardedarray, because you should have eitherthat ora $fillableone - not both.

然后,您应该删除$guarded阵列,因为你应该有要么一个$fillable一个-不能同时使用。

The reason you're getting the error is probably because you added the _tokenfield to your $guardedarray - now, Laravel thinks it's a column on your model even though it isn't.

您收到错误的原因可能是因为您将该_token字段添加到$guarded数组中 - 现在,Laravel 认为它是您模型中的一列,即使它不是。

回答by Kabir Hossain

In this situation I like to use

在这种情况下,我喜欢使用

        $inputs = Request::only('field1','field2');    

For you

为你

$inputs = Request::only('title',
    'subtitle',
    'content',
    'meta_desc',
    'meta_title',
    'seo_title',
    'tags',
    'slug',
    'updated_at',
    'added_on');

回答by Haran singh Kharte

First you assign to a variable Input::all(). Input all is an array. Then You unset the Key _token and pass assigned variable to update. e.g.

首先分配给变量 Input::all()。输入 all 是一个数组。然后您取消设置 Key _token 并传递分配的变量以进行更新。例如

$input = Input::all();
unset($input['_token']);
Client::where('id', $id)->update($input);  

Sorry for my english.

对不起我的英语不好。