问题
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