A Laravel package for generating custom auto-incrementing IDs with prefixes, suffixes, and date-based placeholders.
- Auto-increment ID generation for any column.
- Allow custom prefixes & suffixes & padding
- Supports {DATE}, {MONTH}, and {YEAR} placeholders.
Install via Composer:
composer require omaressaouaf/laravel-id-generator
Generate an ID with a prefix and suffix and padding:
use Omaressaouaf\LaravelIdGenerator\IdGenerator;
use App\Models\User;
$id = IdGenerator::generate(Invoice::class, 'column_name', 5, 'INV-', '-2024');
echo $id; // INV-00001-2024
{DATE}
→2025-02-28
{MONTH}
→2025-02
{YEAR}
→2025
$id = IdGenerator::generate(Invoice::class, 'column_name', 5, 'INV-{YEAR}-');
echo $id; // INV-2025-00001
After installation, you can publish the package configuration file using:
php artisan vendor:publish --provider="Omaressaouaf\LaravelIdGenerator\LaravelIdGeneratorServiceProvider"
This will create a configuration file in config/laravel-id-generator.php
, allowing you define custom generators for different tables or models.
return [
Invoice::class => [
'field' => 'number',
'padding' => 5,
'prefix' => 'INV-',
'suffix' => '-{YEAR}'
],
'receipts' => [
'field' => 'number',
'padding' => 3,
'prefix' => 'RC-',
]
];
Then you can use the generator
$id = IdGenerator::generateFromConfig(Invoice::class);
echo $id; // INV-00001-2025
Run unit tests:
composer test
This package is licensed under the MIT License.