Laravel雄辩 - $ fillable不工作? [英] Laravel Eloquent - $fillable is not working?

查看:321
本文介绍了Laravel雄辩 - $ fillable不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的模型中设置了变量 $ fillable 。我想测试更新功能,我收到这个错误:

I have set the variable $fillable in my model. I wanted to test the update functionality, and I get this error:


SQLSTATE [42S22]:未找到列:1054'字段列表'中的未知列'_method'(SQL:update position set name =休闲水上领袖, _method = PUT, id = 2, description =这是我的描述, updated_at = 2014-05-29 17:05:11其中职位 client_id = 1和 id = 2)

SQLSTATE[42S22]: Column not found: 1054 Unknown column '_method' in 'field list' (SQL: update positions set 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)"

为什么当我的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不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆