Laravel - 批量分配异常错误
我正在尝试将多行保存到一个表中,但是,我遇到了一个质量分配错误
.
I am trying to save multiple rows to a table, however, I am presented with a Mass Assignment Error
.
错误是:IlluminateDatabaseEloquentMassAssignmentExceptioncriteria_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();
}
我的数据库结构是:
所以没有任何不正确的拼写.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);
}
相关文章