PHPackages                             rawaby88/muid - 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. rawaby88/muid

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

rawaby88/muid
=============

Laravel package for generating prefixed, validatable unique identifiers similar to Stripe's ID format

v5.0.2(5mo ago)1417MITPHPPHP ^8.1CI passing

Since Jan 18Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/rawaby88/muid)[ Packagist](https://packagist.org/packages/rawaby88/muid)[ RSS](/packages/rawaby88-muid/feed)WikiDiscussions main Synced today

READMEChangelog (2)Dependencies (4)Versions (4)Used By (0)

MUID - Model Unique Identifier for Laravel
==========================================

[](#muid---model-unique-identifier-for-laravel)

A Laravel package for generating prefixed, validatable unique identifiers similar to Stripe's ID format.

```
┌─────────────────────────────────────────────────────────────────────────┐
│                           MUID FORMAT OVERVIEW                          │
├─────────────────────────────────────────────────────────────────────────┤
│                                                                         │
│  ORDERED (time-sortable, like Stripe)                                   │
│  ┌──────┬───┬──────────┬─────────────────────────┐                      │
│  │ usr  │ _ │ 2HNtGjK8 │ mX4pQ7rS9vWxYz1234     │  → VARCHAR(36)       │
│  └──────┴───┴──────────┴─────────────────────────┘                      │
│  prefix(4) │ timestamp(8) │ random(23)                                  │
│                                                                         │
│  PADDED (human-readable sequential)                                     │
│  ┌──────┬───┬───────────┐                                               │
│  │ inv  │ _ │ 0000042   │  → BIGINT (stores: 42)                        │
│  └──────┴───┴───────────┘                                               │
│  prefix(3) │ padded sequence(7)                                         │
│                                                                         │
│  INCREMENTAL (simple sequential)                                        │
│  ┌──────┬───┬────┐                                                      │
│  │ ord  │ _ │ 42 │  → BIGINT (stores: 42)                               │
│  └──────┴───┴────┘                                                      │
│  prefix(3) │ sequence                                                   │
│                                                                         │
└─────────────────────────────────────────────────────────────────────────┘

```

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Quick Comparison](#quick-comparison)
- [Strategy Overview](#strategy-overview)
- [HasMuid Trait (String-based)](#hasmuid-trait-string-based)
- [HasIntegerMuid Trait (Integer-based)](#hasintegermuid-trait-integer-based)
- [Migration Macros](#migration-macros)
- [Real-World Scenarios](#real-world-scenarios)
- [Validation](#validation)
- [Parsing &amp; Extracting](#parsing--extracting)
- [Configuration](#configuration)
- [API Reference](#api-reference)

---

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

[](#installation)

```
composer require rawaby88/muid
```

Publish the configuration:

```
php artisan vendor:publish --tag=muid-config
```

---

Quick Comparison
----------------

[](#quick-comparison)

Aspect`HasMuid` (Ordered)`HasIntegerMuid` (Padded/Incremental)**Database Type**`VARCHAR(36)``BIGINT AUTO_INCREMENT`**Example ID**`usr_2HNtGjK8mX4pQ7rS``inv_0000042`**Storage in DB**`usr_2HNtGjK8mX4pQ7rS``42`**Generated By**PHP (timestamp + random)Database (auto-increment)**Race Conditions**None (unique by design)None (DB handles it)**Sortable**Yes (time-based)Yes (sequential)**Human Readable**NoYes**Best For**Users, Products, PostsInvoices, Orders, Tickets---

Strategy Overview
-----------------

[](#strategy-overview)

### Available Strategies

[](#available-strategies)

```
┌─────────────────────────────────────────────────────────────────────────────┐
│                              MUID STRATEGIES                                 │
├─────────────────┬───────────────┬──────────────┬────────────────────────────┤
│ Strategy        │ Trait         │ Storage      │ Output Example             │
├─────────────────┼───────────────┼──────────────┼────────────────────────────┤
│ ordered         │ HasMuid       │ VARCHAR(36)  │ usr_2HNtGjK8mX4pQ7rS9vWx   │
│ incremental     │ HasIntegerMuid│ BIGINT       │ ord_1, ord_2, ord_3        │
│ padded          │ HasIntegerMuid│ BIGINT       │ inv_0000001, inv_0000002   │
└─────────────────┴───────────────┴──────────────┴────────────────────────────┘

```

### How Each Strategy Stores Data

[](#how-each-strategy-stores-data)

#### Ordered Strategy (HasMuid)

[](#ordered-strategy-hasmuid)

```
┌──────────────────────────────────────────────────────────────┐
│                    DATABASE TABLE: users                      │
├──────────────────────────────────┬───────────────────────────┤
│ id (VARCHAR 36)                  │ name                      │
├──────────────────────────────────┼───────────────────────────┤
│ usr_2HNtGjK8mX4pQ7rS9vWx         │ John Doe                  │
│ usr_2HNtGjL3nY5qR8sT0uVy         │ Jane Smith                │
│ usr_2HNtGjM9oZ6rS9tU1vWz         │ Bob Wilson                │
└──────────────────────────────────┴───────────────────────────┘
                    ↓
         What you see in PHP:
         $user->id = "usr_2HNtGjK8mX4pQ7rS9vWx"

```

#### Incremental Strategy (HasIntegerMuid)

[](#incremental-strategy-hasintegermuid)

```
┌──────────────────────────────────────────────────────────────┐
│                    DATABASE TABLE: orders                     │
├──────────────────────────────────┬───────────────────────────┤
│ id (BIGINT AUTO_INCREMENT)       │ total                     │
├──────────────────────────────────┼───────────────────────────┤
│ 1                                │ 99.99                     │
│ 2                                │ 149.50                    │
│ 3                                │ 75.00                     │
└──────────────────────────────────┴───────────────────────────┘
                    ↓
         What you see in PHP:
         $order->id   = 1           (raw integer)
         $order->muid = "ord_1"     (formatted with prefix)

```

#### Padded Strategy (HasIntegerMuid)

[](#padded-strategy-hasintegermuid)

```
┌──────────────────────────────────────────────────────────────┐
│                   DATABASE TABLE: invoices                    │
├──────────────────────────────────┬───────────────────────────┤
│ id (BIGINT AUTO_INCREMENT)       │ amount                    │
├──────────────────────────────────┼───────────────────────────┤
│ 1                                │ 500.00                    │
│ 2                                │ 1250.00                   │
│ 42                               │ 899.99                    │
└──────────────────────────────────┴───────────────────────────┘
                    ↓
         What you see in PHP:
         $invoice->id   = 42              (raw integer)
         $invoice->muid = "inv_0000042"   (formatted with padding)

```

---

HasMuid Trait (String-based)
----------------------------

[](#hasmuid-trait-string-based)

### When to Use

[](#when-to-use)

✅ **Use `HasMuid` for:**

- User accounts
- Products / Items
- Posts / Articles
- Comments
- Any entity that needs globally unique IDs
- Distributed systems
- When you need time-sortable IDs

### Basic Usage

[](#basic-usage)

```
