我正在尝试将多行保存到一个表中,但是,我遇到了一个质量分配错误.
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模板网!
PHP、MySQL PDOException 的死锁异常代码?Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死锁异常代码?)
PHP PDO MySQL 可滚动游标不起作用PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滚动游标不起作用)
PHP PDO ODBC 连接PHP PDO ODBC connection(PHP PDO ODBC 连接)
使用 PDO::FETCH_CLASS 和魔术方法Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔术方法)
php pdo 只从 mysql 获取一个值;等于变量的值php pdo get only one value from mysql; value that equals to variable(php pdo 只从 mysql 获取一个值;等于变量的值)
MSSQL PDO 找不到驱动程序MSSQL PDO could not find driver(MSSQL PDO 找不到驱动程序)