Laravel 'field list' 中的未知列“_token”

Laravel Unknown column '_token' in 'field list'

我注意到一些 Laravel 应用程序有奇怪的行为。

当我运行让我说

Users::where("id",$request->input("id"))->update($request->input());

有时一切顺利。在其他情况下,我得到

 Unknown column '_token' in 'field list'

所以有时它只读取 $fillable 参数中设置的内容,而其他时候它会从 $request->input() 中获取所有内容。我一直在比较不同的模型,看不出有什么区别。我知道我可以使用 $request->only([]) 方法来解决这个问题,但是是否有其他人遇到过这个问题并且可能知道背后的原因?

编辑

这是我的model.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class BookingRequests extends Model
{
    //
    protected $fillable = array(
        "account_id",
        "quote_id",
        "booking_id",
        "website_id",
        "color",
        "custom_group_name",
        "is_confirmed",
        "ready_to_issue",
        "created_by",
    );


    /**
     * Return Quote
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function Quote(){
        return $this->belongsTo('App\Quotes',"quote_id","id");
    }
}

这是我的控制器

/**
     * Update Booking Reques
     * @param Request $request
     */
    public function update(Request $request){
        /**
         * Validate
         */
        $this->validate($request,array(
            "id" => "required"
        ));
        /**
         * Update
         */
        BookingRequests::where("id",$request->input("id"))->update($request->input());
        /**
         * Return
         */
        return redirect()->back()->with("success","Booking updated");
    }

运行 Laravel 5.3.31

你可以使用 $request->except('_token'); 顺便说一句,检查两次,所有数据都是预期的。

它不是 sometimes this error happens$fillable 只适用于批量分配 creation/insertion 不在更新中,所以当你更新它时包括列 _token.

说实话你现在做的事情真的很冒险。事实上,现在可以更新任何字段,无论 $fillable 属性。这是因为你现在是这样更新的:

Users::where("id",$request->input("id"))->update($request->input());

当你像这样进行更新时,实际上你是在直接在数据库中进行更新,Eloquent 没有使用任何东西,所以执行的查询看起来像这样:

UPDATE users SET a=1, b=2 WHERE id = 5

因此,如果有人在此 table 中发送现有专栏,他们将被更新,这是非常非常危险的,因为您不希望任何人修改您不想修改的专栏。

但是如果你这样做:

$user = Users::where("id",$request->input("id"))->firstOrFail();
$user->update($request->input());

在上面的例子中使用了 Eloquent(首先你在数据库中找到记录并且它是 Eloquent 模型然后你尝试更新这个 Eloquent 模型),所以现在可以仅更新 $fillable 中的字段(假设您使用的是 'fillable-way' 但您确实在查看模型)。所以现在,无论请求中发送什么,都只会更新 $fillable 中的字段。

显然上面可以写得更短一些:

$user = Users::findOrFail($request->input("id"));
$user->update($request->all());

甚至像这样:

Users::findOrFail($request->input("id"))->update($request->all());