问题描述
我已经在我的模型中设置了变量 $ fillable
。我想测试更新
功能,我收到这个错误:
I have set the variable $fillable
in my model. I wanted to test the update
functionality, and I get this error:
为什么当我的fillable没有作为参数时,为什么在$ code> _method 这个叫喊?我的更新功能是:
Why is this yelling at _method
when my fillable doesn't have that as a parameter? My update function is:
Client::find($client_id)
->positions()
->whereId($id)
->update(Input::all());
推荐答案
更改以下内容:
->update(Input::all());
到这个(从数组中排除 _method
)
to this (exclude the _method
from the array)
->update(Input::except('_method'));
更新:
c $ c> update 方法正在从中调用... Illuminate\Database\Eloquent\Builder
由 _call
方法 Illuminate\Database\Eloquent\Relations
类(因为你正在调用更新
关系),因此 $ fillable
检查未执行,您可以使用 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:
public function update(array $values)
{
return $this->query->update($this->addUpdatedAtColumn($values));
}
如果直接在Model(不是关系)上调用这个: / p>
If you directly call this on a Model (Not on a relation):
Positions::find($id)->update(Input::all());
然后这不会发生,因为 fillable
check将在 Model.php
中执行,因为以下更新
方法将从中调用Illuminate\数据库\Eloquent\Model
class:
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:
public function update(array $attributes = array())
{
if ( ! $this->exists)
{
return $this->newQuery()->update($attributes);
}
return $this->fill($attributes)->save();
}
这篇关于Laravel雄辩 - $ fillable不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!