PHPackages                             alimarchal/id-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. alimarchal/id-generator

ActiveLaravel-package[Utility &amp; Helpers](/categories/utility)

alimarchal/id-generator
=======================

A Laravel package to generate unique, prefixed IDs with database transaction safety

v1.1.0(8mo ago)0653MITPHPPHP ^8.2CI passing

Since Aug 14Pushed 8mo agoCompare

[ Source](https://github.com/alimarchal/id-generator)[ Packagist](https://packagist.org/packages/alimarchal/id-generator)[ RSS](/packages/alimarchal-id-generator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (3)Used By (0)

Laravel ID Generator
====================

[](#laravel-id-generator)

A powerful Laravel package to generate **unique, prefixed IDs** with database transaction safety and race condition protection. Perfect for invoices, complaints, quotations, orders, and any document that needs professional numbering.

```
// Generate professional IDs instantly
$invoiceId = generateUniqueId('invoice', 'invoices', 'invoice_no');
// Output: INV-20250814-0001

$complaintId = generateUniqueId('complaint', 'complaints', 'complaint_no');
// Output: CMP-20250814-0001
```

🎯 Why You Need This Package
---------------------------

[](#-why-you-need-this-package)

### The Problem

[](#the-problem)

When building applications, you often need to generate unique document numbers like:

- Invoice Numbers: `INV-20250814-0001`
- Complaint IDs: `CMP-20250814-0001`
- Order Numbers: `ORD-20250814-0001`

**Common challenges:**

- **Race Conditions**: Two users creating records simultaneously can get the same number
- **Non-Professional Format**: Simple auto-increment IDs look unprofessional
- **No Business Logic**: Hard to identify document type from the number
- **Maintenance Overhead**: Writing custom numbering logic for each model

### The Solution

[](#the-solution)

This package provides:

- ✅ **Race Condition Safe**: Uses database transactions and row locking
- ✅ **Professional Format**: `PREFIX-YYYYMMDD-XXXX` format
- ✅ **Zero Conflicts**: Guaranteed unique IDs across your entire application
- ✅ **Laravel Native**: Follows Laravel conventions and best practices
- ✅ **Future Proof**: Compatible with Laravel 11, 12, and beyond
- ✅ **Auto-Scalable**: Handles millions of records without performance issues

🚀 Installation
--------------

[](#-installation)

```
composer require alimarchal/id-generator
```

**That's it!** Thanks to Laravel's auto-discovery, the package is immediately ready to use.

📋 Setup (One-Time)
------------------

[](#-setup-one-time)

### 1. Publish and Run Migration

[](#1-publish-and-run-migration)

```
php artisan vendor:publish --tag=id-generator-migrations
php artisan migrate
```

This creates an `id_prefixes` table with sample data:

idnameprefixcreated\_atupdated\_at1invoiceINV......2complaintCMP......3quotationQTN......### 2. Add Your Custom Prefixes (Optional)

[](#2-add-your-custom-prefixes-optional)

```
use Illuminate\Support\Facades\DB;

DB::table('id_prefixes')->insert([
    ['name' => 'order', 'prefix' => 'ORD'],
    ['name' => 'receipt', 'prefix' => 'RCP'],
    ['name' => 'estimate', 'prefix' => 'EST'],
]);
```

🎯 Usage
-------

[](#-usage)

### Method 1: Helper Functions (Recommended)

[](#method-1-helper-functions-recommended)

The simplest way to generate IDs anywhere in your application.

#### Using Database Prefixes

[](#using-database-prefixes)

```
// Generate invoice number using 'invoice' type from database
$invoiceId = generateUniqueId('invoice', 'invoices', 'invoice_no');
// Output: INV-20250814-0001

// Generate complaint number
$complaintId = generateUniqueId('complaint', 'complaints', 'complaint_no');
// Output: CMP-20250814-0001

// Generate quotation number
$quotationId = generateUniqueId('quotation', 'quotations', 'quotation_no');
// Output: QTN-20250814-0001
```

#### Using Direct Prefixes

[](#using-direct-prefixes)

```
// Generate ID with custom prefix (no database lookup)
$orderId = generateUniqueIdWithPrefix('ORD', 'orders', 'order_no');
// Output: ORD-20250814-0001

$customId = generateUniqueIdWithPrefix('CUST', 'customers', 'customer_id');
// Output: CUST-20250814-0001
```

### Method 2: Dependency Injection

[](#method-2-dependency-injection)

Perfect when you need more control or are following SOLID principles.

```
