Use of lockForUpdate()
Locking the database between read and update of a database record
$invoice = DB::transaction(function () use ($tenant) {
$inv = DB::table('tenants')
->where('id', $tenant)
->lockForUpdate()
->first('next_invoice')
->next_invoice;
DB::table('tenants')
->where('id', $tenant)
->update(['next_invoice' => ++$inv]);
return $inv;
});Last updated