Create Guaranteed Unique Invoice Number in Laravel
Not as simple as it may sound. How to avoid problems from race conditions.
Problems Considered
Determine the current financial year
$dt = today()->setMonth(Carbon::APRIL)->setDay(1);
if($dt > today()) {
$dt->subYear(1);
}Reset the count in each Financial Year
Model and Migration
Eloquent FirstOrCreate
Safely Increment the invoice number
Putting it all together
Last updated