問(wèn)題描述
我正在嘗試將多行保存到一個(gè)表中,但是,我遇到了一個(gè)質(zhì)量分配錯(cuò)誤
.
I am trying to save multiple rows to a table, however, I am presented with a Mass Assignment Error
.
錯(cuò)誤是: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();
}
我的數(shù)據(jù)庫(kù)結(jié)構(gòu)是:
所以沒(méi)有任何不正確的拼寫(xiě).criteria_id 來(lái)自最近保存的標(biāo)準(zhǔn)的變量(參見(jiàn)上面 forloop 的代碼).
so there isn't any incorrect spelling. The criteria_id comes from the variable from the recently saved criteria (see code above forloop).
任何幫助將不勝感激.
推薦答案
為了能夠通過(guò)將屬性傳遞給模型的構(gòu)造函數(shù)來(lái)設(shè)置屬性,您需要在 $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
方法.它創(chuàng)建了一個(gè)新模型并直接保存:
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);
}
這篇關(guān)于Laravel - 批量分配異常錯(cuò)誤的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!