Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
use Illuminate\Support\Facades\DB; // Auto transaction $order = DB::transaction(function () use ($cart) { $order = Order::create(["user_id" => auth()->id(), "total" => $cart->total()]); foreach ($cart->items as $item) { $order->items()->create($item); Product::where("id", $item->product_id)->decrement("stock", $item->qty); } return $order; }, attempts: 3); // retry up to 3 times on deadlock // Manual transaction DB::beginTransaction(); try { ...; DB::commit(); } catch (\Exception $e) { DB::rollBack(); throw $e; }
Result
Open