alimarchal/id-generator
Generate unique, professional document IDs in Laravel (PREFIX-YYYYMMDD-XXXX) with transaction safety and race-condition protection. Ideal for invoices, complaints, orders, quotations, and more. Compatible with Laravel 11/12, scalable for high volume.
Install via Composer:
composer require alimarchal/id-generator
First Use Case: Generate a unique ID for an e-commerce order with prefix:
use Alimarchal\IdGenerator\Facades\IdGenerator;
$orderId = IdGenerator::generate('ORD', 'orders', 'order_no');
// Output: ORD-20250814-0001
Where to Look First:
1. Basic ID Generation:
// Simple ID (e.g., invoice)
$invoiceId = IdGenerator::generate('invoice', 'invoices', 'invoice_no');
// Prefixed ID (e.g., order)
$orderId = IdGenerator::generateWithPrefix('ORD', 'orders', 'order_no');
2. Service Provider Integration:
// Register in config/app.php
'providers' => [
Alimarchal\IdGenerator\IdGeneratorServiceProvider::class,
],
// Use facade in controllers
use Alimarchal\IdGenerator\Facades\IdGenerator;
3. High-Volume Scenarios:
// Thread-safe for concurrent requests
for ($i = 0; $i < 1000; $i++) {
$id = IdGenerator::generate('invoice', 'invoices', 'invoice_no');
// Guaranteed uniqueness
}
4. Custom Logic Extension:
// Extend via service provider bindings
$this->app->bind('custom.id', function ($app) {
return new CustomIdGenerator($app['id-generator']);
});
order_no, invoice_no columns are indexed for performance.Rule::unique() with the generated ID in models:
$request->validate([
'order_no' => 'required|unique:orders,order_no',
]);
Race Conditions (Mitigated):
DB::transaction() wrappers—let the package handle it.Prefix Collisions:
ORD, TKT) are globally unique across your application.{ENTITY_TYPE}-{SYSTEM} (e.g., ORD-ECOM, TKT-SUPPORT).PHP 8.2+ Requirement:
v1.0.0 via composer require alimarchal/id-generator:^1.0.0.SQLite Testing Quirks:
ORD-20250814-123456).config/id-generator.php to log generation attempts:
'debug' => env('ID_GENERATOR_DEBUG', false),
Custom ID Formats:
Override the default YYYYMMDD-SEQUENCE format via config:
'format' => 'SEQ-{YEAR}', // Output: SEQ-2025
Event Listeners: Publish and extend the package’s events:
php artisan vendor:publish --provider="Alimarchal\IdGenerator\IdGeneratorServiceProvider" --tag="events"
Testing: Use the provided Pest test suite as a template for your own tests:
use Alimarchal\IdGenerator\Tests\TestCase;
test('generates unique IDs', function () {
$id = IdGenerator::generate('test', 'tests', 'test_no');
$this->assertMatchesRegularExpression('/^TEST-\d{8}-\d{4}$/', $id);
});
(prefix, date, sequence) for large tables:
CREATE INDEX idx_unique_ids ON orders(prefix, order_no);
How can I help you explore Laravel packages today?