Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent: merge model with Input

I would like to know how it is possible to merge data from Input::all() with a model and save the result.

To clarify: I would like to do something like below:

$product = Product::find(1); // Eloquent Model

$product->merge( Input::all() ); // This is what I am looking for :)

$product->save();
like image 841
user3518571 Avatar asked Jun 12 '14 09:06

user3518571


2 Answers

You should use update method:

$product->update(Input::all());

But I recommend to use only method instead

$product->update(Input::only('name', 'type...'));
like image 110
Razor Avatar answered Oct 13 '22 01:10

Razor


Use the model's fill() method for greater control. This lets us change attributes after merging the values before we save:

$product->fill($request->all());
$product->foo = 'bar';
$product->save();

If we've properly defined the model's $fillable attributes, there's no need to use Input::only(...) (or $request->only(...) in newer versions).

like image 4
Cy Rossignol Avatar answered Oct 13 '22 00:10

Cy Rossignol