Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel雄辩-$fillable不起作用?_Laravel_Eloquent - Fatal编程技术网

Laravel雄辩-$fillable不起作用?

Laravel雄辩-$fillable不起作用?,laravel,eloquent,Laravel,Eloquent,我已在模型中设置变量$filleble。我想测试更新功能,但出现以下错误: SQLSTATE[42S22]:未找到列:“字段列表”中的1054未知列“\u方法”(SQL:updatepositionssetname=临时水生领袖,方法=放置,id=2,描述=这是我的描述,更新时间=2014-05-29 17:05:11其中positions客户id=1和id=2) 当我的fillable没有参数时,为什么会对\u方法大喊大叫?我的更新功能是: Client::find($client_id)

我已在模型中设置变量
$filleble
。我想测试
更新
功能,但出现以下错误:

SQLSTATE[42S22]:未找到列:“字段列表”中的1054未知列“\u方法”(SQL:update
positions
set
name
=临时水生领袖,
方法
=放置,
id
=2,
描述
=这是我的描述,
更新时间
=2014-05-29 17:05:11其中
positions
客户id=1和
id
=2)

当我的fillable没有参数时,为什么会对
\u方法
大喊大叫?我的更新功能是:

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

->update(Input::all());
为此(从数组中排除
\u方法

更新: 实际上,下面的
update
方法是从
illighte\Database\Eloquent\Builder
类调用的,该类由
\u调用
方法的
illighte\Database\Eloquent\Relations
类触发(因为您正在对关系调用
update
)因此,
$filleble
检查无法执行,您可以使用
Input::except(“\u方法”)
,正如我回答的:

public function update(array $values)
{
    return $this->query->update($this->addUpdatedAtColumn($values));
}
如果直接对模型(而不是关系)调用此函数:

这样就不会发生这种情况,因为
filleable
检查将在
Model.php中执行,因为以下
update
方法将从
illighted\Database\Eloquent\Model
类调用:

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

    return $this->fill($attributes)->save();
}
编写一个父类

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;
}

在从Laravel 5.2升级到5.4之后,我经历了这一突破——我在文档/迁移指南中找不到任何相关内容

正如本节所述,正确的修复/使用Eloquent似乎是:

Positions::find($id)->fill(Input::all())->save();
要触发laravel的
可填充检查,然后查看更改。

您也可以这样做

$data=request()->all();
//将它们从阵列中删除
未设置($data[“U标记”],$data[“U方法”);
//然后
客户端::查找($Client\u id)
->职位()
->whereId($id)
->更新(数据);

这将从数组中删除
\u方法
\u令牌
,这是我最初的测试邮递员。实际上,我还有很多其他密钥要发送。这个
$filleble
的全部要点是,我不必这样做!你是如何使用/set
fulleble
属性的,它在哪里?在我的模型文件中,我有e> protected$filleble=array('name'、'show_order'、'description');
您的模型中是否也声明了
$guarded
属性?不,我没有。我需要吗?
->update(Input::except('_method'));
Positions::find($id)->fill(Input::all())->save();