我创建了一个名为 Author 的模型。我尝试在 Eloquent 创建方法的帮助下创建一个模型,如下所示:
public function postCreate(){
Author::create(array(
'user' => Input::get('user'),
'info' => Input::get('info')
));
return Redirect::to('authors')
->with('message', 'User created successfully');
}
“user”和“info”是表单元素的名称。我确信我没有错字。当我运行此命令时,未创建模型并显示 MassAssignmentException。
但是当我尝试使用以下方法时,模型已创建并保存在表中
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');
}
我真的很想使用 create 方法,它看起来更干净、更简单。
最佳答案
这应该适合你:
1) 正如 @fideloper 和 @the-shift-exchange 已经列出的那样,在您的作者模型中,您需要创建以下字段(这是您希望可用于自动填充的所有数据库列的白名单 [mass]作业])
protected $fillable = array('user','info', ... ,'someotherfield');
2)使用下面的代码来触发批量分配机制
$author = new Author;
$author->fill(Input::all());
$author->save();
关于laravel - 无法使用 Eloquent create 方法创建模型。错误提示 MassAssignMentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18699866/