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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With