沃梦达 / 编程问答 / php问题 / 正文

Laravel - 批量分配异常错误

Laravel - Mass Assignment Exception error(Laravel - 批量分配异常错误)

本文介绍了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);
}

这篇关于Laravel - 批量分配异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Laravel - 批量分配异常错误

基础教程推荐