PHPackages                             luttje/laravel-user-custom-id - 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. luttje/laravel-user-custom-id

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

luttje/laravel-user-custom-id
=============================

Let your users configure an ID style and then use that ID style in your application.

0.4.0(2mo ago)13MITPHP ^8.3

Since Dec 30Compare

[ Source](https://github.com/luttje/laravel-user-custom-id)[ Packagist](https://packagist.org/packages/luttje/laravel-user-custom-id)[ Docs](https://github.com/luttje/laravel-user-custom-id)[ GitHub Sponsors](https://github.com/luttje)[ RSS](/packages/luttje-laravel-user-custom-id/feed)WikiDiscussions Synced today

READMEChangelog (4)Dependencies (31)Versions (7)Used By (0)

🏷 Laravel User Custom ID
========================

[](#-laravel-user-custom-id)

**This Laravel package enables users to create and utilize custom ID styles within your application.**For instance, a user like Jane can set a format such as `product-{attribute-owner:name}-{random:5}-{increment}` for product IDs. Once configured, the application can generate IDs for Jane's products, like `product-Jane-z4AyW-1` for the first product, `product-Jane-3QJ5A-2` for the second product, etc.

[![run-tests](https://github.com/luttje/laravel-user-custom-id/actions/workflows/run-tests.yml/badge.svg)](https://github.com/luttje/laravel-user-custom-id/actions/workflows/run-tests.yml)[![Coverage Status](https://camo.githubusercontent.com/f3e649027e18e430c5da7d9288eae4251ae9c3b56900331ad1abdd88392211a4/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6c7574746a652f6c61726176656c2d757365722d637573746f6d2d69642f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/luttje/laravel-user-custom-id?branch=main)

Warning

This package is still in development. It is not yet ready for production use and the API may change at any time.

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

[](#installation)

You can install the package via composer:

```
composer require luttje/laravel-user-custom-id
```

👨‍🔧 Usage
---------

[](#‍-usage)

### Scenario

[](#scenario)

Imagine a webshop system with multiple shops. Users need customizable product IDs. For example:

- User A prefers sequential numbers like `123`, `124`, `125`.
- User B opts for a prefix with numbers like `A-123`, `A-124`.
- User Jane likes a complex format with ids like `product-Jane-z4AyW-1`, `product-Jane-3QJ5A-2`.

This package allows users to define their own ID style. The application can then generate ID's for their products based on the style they chose.

### Configuration

[](#configuration)

Users define their ID style for a model, usually upon creating their shop. They select the model they want to customize an id style for (e.g., `Product`) and input their desired style (e.g., `product-{attribute-owner:name}-{random:5}-{increment}`). The configuration is then created as follows:

```
$user = auth()->user();

UserCustomId::createFormat(
    Product::class,
    $user,
    'product-{attribute-owner:name}-{random:5}-{increment}',
    'custom_id'
);
```

This setup requires the target model (e.g., `Product`), the user, the ID style, and the attribute to store the ID (`custom_id`). Whenever a product belonging to the user is created, the ID will be generated according to this format. It will be stored in the `custom_id` attribute of the product.

### ID Generation

[](#id-generation)

There are two methods to generate IDs:

#### 1. ✋ Manual ID generation

[](#1--manual-id-generation)

Generate an ID when a user creates a new product:

```
$user = auth()->user(); // Logged in as 'Jane'

$product = new Product([
    'name' => 'Jacket',
    'slug' => 'jacket',
    'description' => 'Hand crafted jacket, by me (Jane).',
]);

UserCustomId::generateFor($product, $user);

$product->save();
```

Based on the configuration the user created earlier, the ID will be generated as something like `product-Jane-FXALW-1`. It will be saved in the `custom_id` attribute of the product. If the user creates another product, its ID will be something akin to `product-Jane-3QJ5A-2`.

#### 2. 🤖 Automatically generating an ID

[](#2--automatically-generating-an-id)

Apply the `HasUserCustomId` interface and `WithUserCustomId` trait to your model for auto-generation:

```
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
use Luttje\UserCustomId\Contracts\HasUserCustomId;
use Luttje\UserCustomId\Traits\WithUserCustomId;

class Category extends Model implements HasUserCustomId
{
    use HasUuids;
    use WithUserCustomId;

    protected $fillable = [
        'name',
        'slug',
        'description',
        'owner_id',
    ];

    protected $hidden = [
        'id',
    ];

    public function owner()
    {
        return $this->belongsTo(User::class);
    }

    public function products()
    {
        return $this->hasMany(Product::class);
    }
}
```

Note

**Implement the getOwner() method in your model to define the ID style's owner. By default, it uses the owner relation, but you can customize it.**

With the above implementation an ID will automatically be generated upon [the Eloquent `creating` event](https://laravel.com/docs/10.x/eloquent#events) for the model. It will store the ID in the configured attribute of the model.

### Customizing the Owner Model

[](#customizing-the-owner-model)

You can designate any model as the ID style's owner, not just the logged in user. Provide the owner model as the second argument to generateFor. Or have the `getOwner()` method in your model that implements the `HasUserCustomId` interface return the owner model.

Depending on your situation it might make more sense to have a `Tenant` or `Team` model be the owner of the ID style.

### Understanding Format Chunks

[](#understanding-format-chunks)

Formats are made of "chunks," placeholders for specific values. For example, `product-{attribute-owner:name}-{random:5}-{increment}` contains several chunks:

- `product-`
- `{attribute-owner:name}`: The owner model's name.
- `-`
- `{random:5}`: A random 5-character string.
- `-`
- `{increment}`: An incrementing number.

Each chunk follows the `{name:parameters}` format, with specific parameters for each type. Except for literal chunks, which are just literal strings (like `product-` and the `-` chunks above).

#### Chunk Types

[](#chunk-types)

- `{increment:amount:group-by:group-symbol}`: Incrementing numbers.
- `{attribute-owner:attribute:start:length}`: Owner model attribute value (or a part of it)
- `{random:length:characters}`: Random characters.
- `{attribute-target:attribute:start:length}`: Target model attribute value (or a part of it)
- `{attribute-relation:attribute:start:length}`: Related model attribute value (or a part of it)
- `{uuid:version}`: Universally unique identifier
- Time Chunks: `{year}`, `{month:format}`, `{day}`, etc.

> [» Check out the **🍪 Format Chunks** documentation](docs/format-chunks.md) to learn more about all available chunks and properties.

Testing
-------

[](#testing)

Then run the tests with:

```
composer test
```

License
-------

[](#license)

This package is open-sourced software licensed under the MIT license. Please see the [license file](LICENSE.md) for more information.

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance85

Actively maintained with recent releases

Popularity5

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 60.9% 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.

###  Release Activity

Cadence

Every ~280 days

Total

4

Last Release

76d ago

PHP version history (3 changes)0.1.0PHP ^8.1

0.3.0PHP ^8.1|^8.2|^8.3|^8.4

0.4.0PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/d50c11122445f7743eb6ec278ca6322a0f3d9c9296df290a66c0e5ab0df5e750?d=identicon)[luttje](/maintainers/luttje)

---

Top Contributors

[![luttje](https://avatars.githubusercontent.com/u/2738114?v=4)](https://github.com/luttje "luttje (39 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (16 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (9 commits)")

---

Tags

laravelcustomizableidluttje

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/luttje-laravel-user-custom-id/health.svg)

```
[![Health](https://phpackages.com/badges/luttje-laravel-user-custom-id/health.svg)](https://phpackages.com/packages/luttje-laravel-user-custom-id)
```

###  Alternatives

[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.8M47](/packages/spatie-laravel-pdf)[codewithdennis/filament-select-tree

The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.

329530.5k29](/packages/codewithdennis-filament-select-tree)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

124603.0k](/packages/worksome-exchange)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3914.6k](/packages/rawilk-profile-filament-plugin)[tarfin-labs/event-machine

Event-driven state machines for Laravel with event sourcing, type-safe context, and full audit trail.

199.4k](/packages/tarfin-labs-event-machine)[tapp/filament-form-builder

User facing form builder using Filament components

132.4k3](/packages/tapp-filament-form-builder)

PHPackages © 2026

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