How to keep using condition in laravel?

Basically, I want to save data in my model, using a condition to prevent multiple stored data from being executed simultaneously.

I am currently doing the following:

$order = Order::find(id); $order->paid_amount = $amount; $order->status = $status; $order->save(); 

What I need:

 $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 need to use raw sql or update?

+5
source share
3 answers

I assume that you mean: "Is it possible to do this in an eloquent manner or do I need to use raw sql or update?"

Anyway, I think you need a 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.

+1
source

A simplified version of @Amarnasan's answer:

 Order::findOrFail($id)->update([ 'paid_amount' => $amount, 'status' => $status; ]); 
+3
source

I think this is the best way to do this:

 $order = Order::where('last_update', $last_update)->find($id); $order->paid_amount = $amount; $order->status = $status; $order->save(); 
+1
source

Source: https://habr.com/ru/post/1238621/


All Articles