問題描述
我正在嘗試將多行保存到一個表中,但是,我遇到了一個質量分配錯誤
.
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);
}
這篇關于Laravel - 批量分配異常錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!