Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eloquent update and get record back

When I usually update a record I do

$myObject->update(['field' => 'value']);

And that updates both the database and my instance $myObject. However sometimes I need to do bulk update so I use the model facade and do

$result = MyObject::where(...)->update(['field' => 'value');

The issue here is that $result sends me back a boolean instead of the updated instances so what I usually have to do separately is right after I need to do the same filter but this time a get().

$objects = MyObject::where(...)->get();

Is there a more efficient way to update and get the records in one call / request to the database?

like image 786
user391986 Avatar asked Jun 13 '15 22:06

user391986


1 Answers

This will solve the problem

$product = Product::where('id','76887')->first();

$result = $product->update(['field' => 'value');

$theUpdatedData = $product->refresh(); 

return $theUpdatedData //This return the updated record 
like image 63
Godstime John Avatar answered Oct 27 '22 20:10

Godstime John