Basically I want to save data in my model using condition to prevent multiple save data being performed simultaneously.
What I currently do now is:
$order = Order::find(id);
$order->paid_amount = $amount;
$order->status = $status;
$order->save();
What I need is:
$order = Order::find(id);
$order->paid_amount = $amount;
$order->status = $status;
$order->where('last_update', $last_update);
$order->save();
Is it possible to do this in laravel efficiently or do I have to use raw sql or update?
If you want to use mysql function then you must have to use db raw function. in this example i will show you laravel select query with if condition. In minor case we need to use if else condition in laravel 5, laravel 6, laravel 7 and laravel 8 Eloquent.
How to use if condition in laravel select query? We will learn how to use if condition in laravel select query. we can use mysql case when statement with db raw in laravel. If you want to use mysql function then you must have to use db raw function. in this example i will show you laravel select query with if condition.
You can update the record which you recently saved in your database using save method in Laravel. You can get the id of recently saved record and after that you can use update method with where condition to update the record. Using this code snippet you can update the recently added record.
A simplified version of the @Amarnasan answer:
Order::findOrFail($id)->update([
'paid_amount' => $amount,
'status' => $status;
]);
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