Laravel Eloquent - $fillable 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23945040/
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 09:34:42  来源:igfitidea点击:

Laravel Eloquent - $fillable is not working?

laraveleloquent

提问by Kousha

I have set the variable $fillablein my model. I wanted to test the updatefunctionality, and I get this error:

我已经$fillable在我的模型中设置了变量。我想测试该update功能,但收到此错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '_method' in 'field list' (SQL: update positionsset name= Casual Aquatic Leader, _method= PUT, id= 2, description= Here is my description, updated_at= 2014-05-29 17:05:11 where positions.client_id= 1 and id= 2)"

SQLSTATE[42S22]: Column not found: 1054 Unknown column '_method' in 'field list' (SQL: update positionsset name= Casual Aquatic Leader, _method= PUT, id= 2, description= Here is my description, updated_at= 2014-05-29 17: 05:11 其中positions. client_id= 1 和id= 2)”

Why is this yelling at _methodwhen my fillable doesn't have that as a parameter? My update function is:

_method当我的可填充没有将其作为参数时,为什么会大喊大叫?我的更新功能是:

Client::find($client_id)
        ->positions()
        ->whereId($id)
        ->update(Input::all());

回答by The Alpha

Change following:

更改以下内容:

->update(Input::all());

to this (exclude the _methodfrom the array)

到这个(_method从数组中排除)

->update(Input::except('_method'));

Update:

更新:

Actually following updatemethod is being called from Illuminate\Database\Eloquent\Builderclass which is being triggered by _callmethod of Illuminate\Database\Eloquent\Relationsclass (because you are calling the updateon a relation) and hence the $fillablecheck is not getting performed and you may use Input::except('_method')as I answered:

实际上,update正在从Illuminate\Database\Eloquent\Builder类中调用以下方法,该类是由类的_call方法触发的Illuminate\Database\Eloquent\Relations(因为您正在调用update关系),因此$fillable未执行检查,您可以Input::except('_method')按照我的回答使用:

public function update(array $values)
{
    return $this->query->update($this->addUpdatedAtColumn($values));
}

If you directly call this on a Model (Not on a relation):

如果您直接在模型上调用它(不在关系上):

Positions::find($id)->update(Input::all());

Then this will not happen because fillablecheck will be performed within Model.phpbecause following updatemethod will be called from Illuminate\Database\Eloquent\Modelclass:

那么这不会发生,因为fillable检查将在内部执行,Model.php因为update将从Illuminate\Database\Eloquent\Model类中调用以下方法:

public function update(array $attributes = array())
{
    if ( ! $this->exists)
    {
        return $this->newQuery()->update($attributes);
    }

    return $this->fill($attributes)->save();
}

回答by BeeChen

write a parent class

写一个父类

class BaseModel extends Model

public static function getFillableAttribute(Model $model, $data){
    $array = $model->getFillable();
    $arr = [];
    foreach ($array as $item){
        if( isset($data["$item"])){
            $arr["$item"] = $data["$item"];
        }
    }
    return $arr;
}

回答by nover

I experienced this breaking after updating from Laravel 5.2 to 5.4 - I can't find anything in the documentation / migration guide that covers it.

从 Laravel 5.2 更新到 5.4 后,我遇到了这种中断 - 我在文档/迁移指南中找不到任何涵盖它的内容。

As covered in this github issuethe correct fix/use of Eloquent seems to be:

正如此github 问题中所述,Eloquent 的正确修复/使用似乎是:

Positions::find($id)->fill(Input::all())->save();

To trigger the fillablecheck by laravel and then perist the changes.

fillable通过 laravel触发检查,然后保留更改。

回答by ManuEl Magak

You could also do this

你也可以这样做

$data = request()->all();
//remove them from the array
unset($data['_token'],$data['_method']);
//then
Client::find($client_id)
        ->positions()
        ->whereId($id)
        ->update($data);

This removes the _methodand _tokenfrom the array

这将从数组中删除_method_token