Laravel - 质量分配异常错误 [英] Laravel - Mass Assignment Exception error

查看:127
本文介绍了Laravel - 质量分配异常错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将多行保存到表中,但是,我会看到一个批量分配错误

I am trying to save multiple rows to a table, however, I am presented with a Mass Assignment Error.

错误是: Illuminate \ Database \ Eloquent \ MassAssignmentException criteria_id

$criteria->save();

    $criteria_id = $criteria->id;

     foreach(Input::get('bedrooms') as $bedroom){
        $new_bedroom=array(
            'criteria_id' => $criteria->id,
            'bedroom' => $bedroom,
            );
        $bedroom = new Bedroom($new_bedroom);
        $bedroom->save();
    }

我的数据库结构是:

所以没有' t拼写错误criteria_id来自最近保存的条件的变量(参见上面的代码forloop)。

so there isn't any incorrect spelling. The criteria_id comes from the variable from the recently saved criteria (see code above forloop).

任何帮助将不胜感激。

推荐答案

为了能够通过将它们传递给模型的构造函数来设置属性,您需要在 $ fillable 数组。如文档所述

To be able to set properties by passing them to the model's constructor, you need to list all the properties you need in the $fillable array. As mentioned in the Docs

class Bedroom extends Eloquent {
    protected $fillable = array('criteria_id', 'bedroom');
}

还可以使用 create 方法,如果你想要的它创建一个新模型并直接保存:

Also you can use the create method if you want. It creates a new model and saves it directly:

foreach(Input::get('bedrooms') as $bedroom){
    $new_bedroom=array(
        'criteria_id' => $criteria->id,
        'bedroom' => $bedroom,
        );
    $bedroom = Bedroom::create($new_bedroom);
}

这篇关于Laravel - 质量分配异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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