laravel 无法使用 Eloquent create 方法创建模型。错误告知 MassAssignMentException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18699866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:24:16  来源:igfitidea点击:

Unable to create a model with Eloquent create method. Error telling MassAssignMentException

laravellaravel-4eloquent

提问by Nirmalz Thapaz

I have created a model named, Author. I tried creating a model with the help of eloquent create method like this:

我创建了一个名为 Author 的模型。我尝试在 eloquent create 方法的帮助下创建一个模型,如下所示:

public function postCreate(){
   Author::create(array(
       'user' => Input::get('user'),
       'info' => Input::get('info') 
   ));
   return Redirect::to('authors')
        ->with('message', 'User created successfully');
}

'user' and 'info' are the name of the form elements. I am sure I am not mistaken with typo. When I run this, models is not created and says MassAssignmentException.

'user' 和 'info' 是表单元素的名称。我确定我没有误会错别字。当我运行它时,没有创建模型并显示 MassAssignmentException。

But when I tried with the following method, the model is created and was saved in the table

但是当我尝试使用以下方法时,模型被创建并保存在表中

public function postCreate(){

    $author = new Author;
    $author->name = Input::get('user');
    $author->info= Input::get('info');
    $author->save();

    return Redirect::to('authors')
        ->with('message', 'User created successfully');

}

And I really want to use the create method, its just looks much cleaner and simpler.

我真的很想使用 create 方法,它看起来更简洁更简单。

回答by Gadoma

this should work for you:

这应该适合你:

1) as already listed by @fideloper and @the-shift-exchange, in your Author model you need to create the below field (this is a white-list of all database columns you want to be available for autopopulation [mass assignment] )

1) 正如@fideloper 和@the-shift-exchange 已经列出的那样,在您的作者模型中,您需要创建以下字段(这是您希望可用于自动填充 [批量分配] 的所有数据库列的白名单)

 protected $fillable = array('user','info', ... ,'someotherfield'); 

2) use the below code to fire the mass asssignment mechanism

2)使用下面的代码来触发批量分配机制

$author = new Author;
$author->fill(Input::all());
$author->save();

回答by Amit Garg

I was getting the MassAssignmentException when I have extends my model like this.

当我像这样扩展我的模型时,我得到了 MassAssignmentException。

class Author extends Eloquent {

}

I was trying to insert array like this

我试图插入这样的数组

Author::create($array);//$array was data to insert.

Issue has been resolvewhen I created Author Model as given below.

当我创建如下所示的作者模型时,问题已得到解决

class Author extends Eloquent {
    protected $guarded = array();  // Important
}

Reference https://github.com/aidkit/aidkit/issues/2#issuecomment-21055670

参考https://github.com/aidkit/aidkit/issues/2#issuecomment-21055670

回答by Laurence

You need to set the Mass Assignment fields. In your Author model:

您需要设置批量分配字段。在您的作者模型中:

class Author extends Eloquent {

class Author 扩展了 Eloquent {

protected $fillable = array('name', 'bio');

}

}

回答by fideloper

Your model needs to have the $fillable variable set.

您的模型需要设置 $fillable 变量。

See the documentation on mass-assignmentfor details.

有关详细信息,请参阅有关批量分配的文档。

It will look something like this in your Author model:

在您的 Author 模型中,它看起来像这样:

protected $fillable = array('user', 'info');

回答by MKJ

You need to use protected $fillableproperty by assigning it an array of fields/columns which you want to fill/assign values to. For example you have a model with fields f1, f2, f3 and f4. You want to assign values to f1, f2 and f3 but not to f4then you need to use:

您需要protected $fillable通过为其分配要填充/分配值的字段/列数组来使用属性。例如,您有一个带有 fields 的模型f1, f2, f3 and f4。您要为其分配值,f1, f2 and f3 but not to f4则需要使用:

protected $fillable = ['f1', 'f2', 'f3'];

The above line would allow to pass an array to the:

上面的行将允许将数组传递给:

$mod = Model::create($arr);
$mod->save();

Whatever the $arr array contains, but only f1, f2, and f3will be assigned values (if values exists in the $arr array for f1, f2, f3).

无论 $arr 数组包含什么,但只会f1, f2, and f3被分配值(如果值存在于 中$arr array for f1, f2, f3)。

Hope it will help you and others.

希望它会帮助你和其他人。