如何:在 Laravel 4 中批量分配更新?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17076156/
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
How to: mass assign an update in Laravel 4?
提问by mangonights
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.
我正在处理非常大(大量)的表单字段,因此批量分配用户编辑会很棒。批量分配似乎适用于“create()”,但不适用于查找和保存。
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.
我该怎么做?我宁愿不指定许多表单输入。
回答by Phill Sparks
You should be able to use fill(array $attributes)
...
你应该可以使用fill(array $attributes)
...
$post_data = Input::all();
$formobj = HugeForm::find($id);
$formobj->fill($post_data);
$formobj->save();
回答by cheelahim
In case of mass update it could be written even shorter.
在大规模更新的情况下,它可以写得更短。
$post_data = Input::all();
HugeForm::find($id)->update($post_data);
回答by Kieran Fahy
To allow mass assignment within Laravel you need to add:
要允许在 Laravel 中进行批量分配,您需要添加:
protected $guarded = array();
Into your model. Basically this tells laravel not to protect any fields, you could also use:
进入你的模型。基本上这告诉 laravel 不要保护任何字段,你也可以使用:
protected $fillable = array();
And then set the fields you want to be fillable.
然后设置您想要填写的字段。
Hope this helps
希望这可以帮助