【问题标题】:Laravel Column not found: 1054 Unknown column '_token' in 'field list' (SQL: update `tutorials` set `_token` =Laravel Column not found: 1054 Unknown column '_token' in 'field list' (SQL: update `tutorials` set `_token` =
【发布时间】:2022-02-23 16:39:13
【问题描述】:

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

这是我的编辑视图的形式

{!! 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'];

这是我的 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.'
        );
    }

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

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

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

【问题讨论】:

  • 如果你将方法设置为 PUT Laravel 会自动添加一个隐藏的 _method 字段。它与可填写字段有关,但我不知道为什么/在哪里
  • 点击此链接查看 put 方法。laravel.com/docs/4.2/html#opening-a-form 我认为您需要使用小写的 put
  • 我知道如何创建一个 put 表单 - 即使我使用 POST 我仍然收到错误。
  • 使用此受保护的 $fillable = array('*'); 使所有字段都可以填写然后删除受保护的可填充和保护。然后再试一次

标签: php laravel


【解决方案1】:

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

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

然后,您应该删除 $guarded 数组,因为您应该有 either 那个 $fillable 一个 - 而不是两个。

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

【讨论】:

    【解决方案2】:

    在这种情况下我喜欢使用

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

    为你

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

    【讨论】:

      【解决方案3】:

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

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

      对不起我的英语。

      【讨论】:

        【解决方案4】:

        您可以像这样排除 _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');
        }
        

        【讨论】:

          猜你喜欢
          • 2019-06-26
          • 2011-03-16
          • 1970-01-01
          • 1970-01-01
          • 2020-12-01
          • 2018-09-10
          • 1970-01-01
          • 2016-07-22
          • 2021-12-31
          相关资源
          最近更新 更多