How to: mass assign an update in Laravel 4?

做~自己de王妃 提交于 2019-12-09 07:35:47

问题


This is an for internal app, mass assignment security is not an issue in this case.

I'm dealing with very large (numerous) form fields, so mass assigning the user edits would be great. Mass assignment seems to work fine with 'create()' but not with doing a find & save.

This is what I have:

$post_data = Input::all();
$formobj = HugeForm::find($id);
$formobj->save($post_data);

How do I go about it? I'd rather not specify many dozens of form inputs.


回答1:


You should be able to use fill(array $attributes)...

$post_data = Input::all();
$formobj = HugeForm::find($id);
$formobj->fill($post_data);
$formobj->save();



回答2:


In case of mass update it could be written even shorter.

$post_data = Input::all();
HugeForm::find($id)->update($post_data);



回答3:


To allow mass assignment within Laravel you need to add:

protected $guarded = array();

Into your model. Basically this tells laravel not to protect any fields, you could also use:

protected $fillable = array();

And then set the fields you want to be fillable.

Hope this helps



来源:https://stackoverflow.com/questions/17076156/how-to-mass-assign-an-update-in-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!