Dresan
Dresan

Reputation: 11

Laravel avoid insert duplication of consecutive from another table

I have a billing application, where I have a table for billings a table for billing numbering because the user can have more than one billing numbering and each numbering has its own consecutive, so when the user is going to create a billing he chooses the numbering, at the frontend the system shows the possible consecutive with which the billing will be created, is "possible" because if another user creates a billing before him, he will assign the consecutive. So the consecutive is finally assigned in the backend with this code:

...
$numbering = BillingNumbering::find($request->billing_numbering);

$number = $numbering->current_number + 1;

$billing->number = $number;
$numbering->current_number = $number;
$numbering->used = true;

$numbering->save();

$billing->save();

Where first I get the billing numbering, get the current number and increase in one and immediately update the table for future requests.

The problem is that some cases when multiples users are working and save a billing almost at the same time the billing take the same billing number generating duplicates in billing consecutives.

How can I achieve this without number duplication?

Upvotes: 1

Views: 214

Answers (1)

Manish Rajora
Manish Rajora

Reputation: 50

you can make the billing number unique. If I assume this is your real code. why you are saving 'numbering' first not 'billing'.

Upvotes: 1

Related Questions