try this
'number' =>increment(optional($sale->latest('id')->first())->number)
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
we have sales table and want to add unique sale number in table
here is the code but it's duplicate the numbers with simultaneous requests.
if 5 requests at same time it's not working perfectly because when we fetch last number from database it's give us sometime old number.
$sale->create([
'number' => optional($sale->latest('id')->first())->number + 1,
'amount' => 20,
]);
there are 2 queries same time first fetch old number then increment. but it's not good for simultaneous requests.
any solution here?
thanks
Please or to participate in this conversation.