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
Laravel Eloquent - $fillable is not working?
提问by Kousha
I have set the variable $fillable
in my model. I wanted to test the update
functionality, and I get this error:
我已经$fillable
在我的模型中设置了变量。我想测试该update
功能,但收到此错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '_method' in 'field list' (SQL: update
positions
setname
= Casual Aquatic Leader,_method
= PUT,id
= 2,description
= Here is my description,updated_at
= 2014-05-29 17:05:11 wherepositions
.client_id
= 1 andid
= 2)"
SQLSTATE[42S22]: Column not found: 1054 Unknown column '_method' in 'field list' (SQL: update
positions
setname
= 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 _method
when 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 _method
from the array)
到这个(_method
从数组中排除)
->update(Input::except('_method'));
Update:
更新:
Actually following update
method is being called from Illuminate\Database\Eloquent\Builder
class which is being triggered by _call
method of Illuminate\Database\Eloquent\Relations
class (because you are calling the update
on a relation) and hence the $fillable
check 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 fillable
check will be performed within Model.php
because following update
method will be called from Illuminate\Database\Eloquent\Model
class:
那么这不会发生,因为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 fillable
check 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 _method
and _token
from the array
这将从数组中删除_method
和_token