How to save using condition in laravel?

- 1 answer

Ad

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?

Ad

Answer

Ad

I guess you mean "Is it possible to do this in eloquent efficiently or do I have to use raw sql or update?"

Anyway, I think you need the query builder to do what you want:

DB::table('order')
    ->where('id', $id)
    ->update([
         'paid_amount' => $amount,
         'status' => $status;
      ]);

It is as atomic as you can get.

Ad
source: stackoverflow.com
Ad