15 php laravel eloquent laravel-4
我试图将多行保存到表中,但是,我遇到了一个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();
}
Run Code Online (Sandbox Code Playgroud)
我的数据库结构是:
所以拼写没有错误.criteria_id来自最近保存的标准中的变量(请参阅上面的代码forloop).
任何帮助将不胜感激.
luk*_*ter 38
为了能够通过将属性传递给模型的构造函数来设置属性,您需要列出$fillable
数组中所需的所有属性.如文件中所述
class Bedroom extends Eloquent {
protected $fillable = array('criteria_id', 'bedroom');
}
Run Code Online (Sandbox Code Playgroud)
create
如果需要,您也可以使用该方法.它创建一个新模型并直接保存:
foreach(Input::get('bedrooms') as $bedroom){
$new_bedroom=array(
'criteria_id' => $criteria->id,
'bedroom' => $bedroom,
);
$bedroom = Bedroom::create($new_bedroom);
}
Run Code Online (Sandbox Code Playgroud)
csg*_*000 13
该逆什么卢卡斯的说就是"守卫".您可以只声明哪些是受保护的,而不是"白名单"字段.
例如:
class Bedroom extends Model
{
protected $guarded = ['id'];
}
Run Code Online (Sandbox Code Playgroud)
这对我来说更有用,因为我并不关心大多数领域.
来自Laravel 5.2的文档,但我认为它适用于旧版本.
要允许任何字段,您只需提供一个空数组:
class Bedroom extends Model
{
protected $guarded = [];
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
28300 次 |
最近记录: |