PHPackages                             jahid-facts/laravel-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. jahid-facts/laravel-id-generator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jahid-facts/laravel-id-generator
================================

Atomic, collision-free ID generator for Laravel

00PHP

Since Dec 15Pushed 4mo agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

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

[](#laravel-id-generator)

A **100% safe, atomic, collision-free ID generator for Laravel**.

This package generates **human-readable sequential IDs** such as:

- Invoice numbers
- Booking IDs
- Order numbers
- Membership numbers

Designed to work safely under **high concurrency** without race conditions.

---

Author
------

[](#author)

**Jahid**

---

Why This Package Exists
-----------------------

[](#why-this-package-exists)

Common approaches like `MAX(id) + 1` or `orderBy()->first()` are **not safe** under concurrent requests and will eventually generate **duplicate IDs**.

This package solves that problem by:

- Using **atomic database increments**
- Ensuring **database-level consistency**
- Supporting **date-based sequences**

---

Key Features
------------

[](#key-features)

- ✅ Atomic &amp; concurrency-safe
- ✅ No race conditions
- ✅ Works with existing data
- ✅ Daily sequence reset
- ✅ Supports multiple models &amp; columns
- ✅ Simple helper function
- ✅ Laravel 10 / 11 / 12 compatible
- ✅ MySQL / MariaDB compatible

---

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require jahid-facts/laravel-id-generator
```

---

Database Setup
--------------

[](#database-setup)

Run the migration:

```
php artisan migrate
```

This creates the `id_sequences` table which safely stores sequence counters.

---

Usage
-----

[](#usage)

### Helper Function

[](#helper-function)

```
idgen();
```

---

### Generate Invoice Number

[](#generate-invoice-number)

```
use App\Models\Payment;

static::creating(function ($payment) {
    if (!$payment->invoice_number) {
        $payment->invoice_number = idgen()->daily(
            Payment::class,
            'INV',
            'invoice_number'
        );
    }
});
```

**Output example:**

```
INV-15122025-00001

```

---

### Generate Booking ID (Based on Booking Date)

[](#generate-booking-id-based-on-booking-date)

```
use App\Models\Booking;

static::creating(function ($booking) {
    if (!$booking->booking_id) {
        $booking->booking_id = idgen()->daily(
            Booking::class,
            'BK',
            'booking_id',
            $booking->booking_date
        );
    }
});
```

**Output example:**

```
BK-20122025-00002

```

---

ID Format
---------

[](#id-format)

```
PREFIX-DDMMYYYY-XXXXX

```

PartMeaningPREFIXID type (INV, BK, etc.)DDMMYYYYDateXXXXXZero-padded sequence---

Existing Data (Important)
-------------------------

[](#existing-data-important)

If your database already contains IDs, you **must sync the sequence counters once**.

Without syncing, new IDs may start again from `00001` and cause **duplicate key errors**.

After syncing, the generator will continue from the **highest existing value**.

---

Database Safety
---------------

[](#database-safety)

Always keep **UNIQUE indexes** on generated ID columns:

```
ALTER TABLE payments ADD UNIQUE (invoice_number);
ALTER TABLE bookings ADD UNIQUE (booking_id);
```

This guarantees **database-level protection**.

---

Concurrency Guarantee
---------------------

[](#concurrency-guarantee)

This package uses MySQL atomic increments:

```
LAST_INSERT_ID(counter + 1)
```

This guarantees:

- One request = one unique ID
- Safe across multiple PHP workers
- Safe across multiple servers

---

Supported Versions
------------------

[](#supported-versions)

ComponentVersionPHP8.1+Laravel10 / 11 / 12DatabaseMySQL / MariaDB---

Best Practices
--------------

[](#best-practices)

- Always pass the correct date (e.g. `booking_date`)
- Keep unique indexes
- Sync sequences once for old data
- Do not manually edit sequence counters

---

License
-------

[](#license)

MIT License

---

Credits
-------

[](#credits)

Developed by **Jahid**

Happy coding 🚀

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance50

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity12

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/62b6e505c225ed20b667f6aa8b602e59c8595b941541183eff8bb1735e4acc5c?d=identicon)[jahid-facts](/maintainers/jahid-facts)

---

Top Contributors

[![jahid-facts](https://avatars.githubusercontent.com/u/119929408?v=4)](https://github.com/jahid-facts "jahid-facts (2 commits)")

### Embed Badge

![Health badge](/badges/jahid-facts-laravel-id-generator/health.svg)

```
[![Health](https://phpackages.com/badges/jahid-facts-laravel-id-generator/health.svg)](https://phpackages.com/packages/jahid-facts-laravel-id-generator)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
