Membawa teknologi lebih dekat untuk semua
A Modern Way to Generate Business Document Numbers in Laravel
Generating invoice numbers, transaction references, purchase orders, or other business document numbers may look simple at first. However, once an application starts handling multiple concurrent users, generating sequential document numbers becomes a surprisingly challenging problem.
Many applications still determine the next number by querying the highest value stored in the transaction table. Although this approach is easy to implement, it becomes slower as the table grows and is vulnerable to race conditions when multiple requests generate numbers simultaneously.
To solve this problem, we created Laravel ID Generator, an open source package designed to generate business document numbers efficiently, safely, and with minimal configuration.
Why We Built This Package
While developing Point of Sale systems, Online Shops, Procurement applications, and Qurban management platforms, we repeatedly faced the same requirement.
Every application needed unique document numbers.
Invoices.
Transactions.
Purchase Orders.
Procurement records.
Reference numbers.
Instead of implementing the same logic in every project, we decided to build a reusable Laravel package.
A Different Approach
Laravel ID Generator never scans business tables to determine the next sequence.
Instead, it stores a single lightweight state record for each configured generator inside the id_sequences table.
Formatting options such as prefixes, suffixes, padding, and scope configuration remain inside Laravel configuration files where they belong.
This approach provides several advantages.
- Consistent performance regardless of transaction table size.
- Safe concurrent number generation.
- No scheduler required for monthly or yearly resets.
- Configuration remains version controlled.
Flexible Scope Based Reset
The package supports multiple sequence scopes.
- Global
- Yearly
- Monthly
- Daily
Instead of relying on scheduled tasks, the package automatically detects scope changes during number generation and performs the reset atomically.
Built for Concurrent Applications
One of our primary goals was preventing duplicate document numbers.
Every generation runs inside a database transaction while locking only the corresponding sequence row.
This guarantees that concurrent requests never receive the same document number.
Installation
composer require devaspid/laravel-id-generator
Publish the configuration and migration files, then migrate your database.
php artisan vendor:publish --tag=id-generator-config
php artisan vendor:publish --tag=id-generator-migrations
php artisan migrate
php artisan id-generator:sync
Usage
$invoice->number = IdGenerator::generate('invoice');
Configuration is simple.
'invoice' => [
'prefix' => 'INV-{Y}{m}-',
'padding' => 6,
'scope' => 'monthly',
]
The same generator can be used for transactions, procurement records, purchase orders, quotations, and virtually any business document requiring sequential numbering.
Read More Full Documentation
devaspid/laravel-id-generator
Open Source for the Laravel Community
Laravel ID Generator is one of the first open source contributions from Dev ASP ID.
We hope this package helps Laravel developers build business applications that are faster, safer, and easier to maintain.
Feedback, bug reports, feature requests, and pull requests are always welcome.
Happy coding.





