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


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

如果我更新表单模型绑定,则会出现上述错误。我已经将所有数据库字段都放在可填写字段中,而不会影响我仍然收到错误。

这是我的编辑视图的形式

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

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

 /**
     * 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'];

这是我的教程控制器

public function update(Tutorial $tutorial)
    {
        $input = Input::except('_method'); // Request::all() is not working
        $tutorial->update($input);
        return Redirect::route('tutorials.index')->withSuccess(
            'success.'
        );
    }

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

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

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

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

然后,您应该删除$guarded数组,因为您应该拥有该数组$fillable数组 - 而不是两者兼而有之。

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

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

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

给你的

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

首先,你赋值一个变量 Input::all()。输入 all 是一个数组。然后,取消设置键_token并传递要更新的分配变量。例如

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

对不起我的英语。

你可以像这样排除_method和_token

public function update(Request $request, $id)
{
    product::whereId($id)->update($request->except('_token','_method'));
    return redirect('/product')->with('completed', 'customer has been updated');
}