New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

mcandylab/laravel-cuid2

CUID2 support for Laravel
1,094 3

Larablocks Analysis

mcandylab/laravel-cuid2 works with Laravel 12 and 13 (latest release v3.0.1, Aug 1, 2026).

Promising: maintained and up to date, meets 2 of 4 checks; not yet widely used.

  • Works with Laravel 13, the current version
  • Released in the last 12 months (Aug 1, 2026)
  • 218 downloads a month (needs 10,000)
  • 3 GitHub stars (needs 100)

State

Actively maintained
  • Last stable release: Aug 1, 2026.
  • 5 releases in the last 12 months.
  • First released: Jul 12, 2026.
  • Monthly downloads: 218.
  • 0 open issues on GitHub.

Data from Packagist and GitHub, checked by Larablocks on Nov 29, 2026.

Install
composer require mcandylab/laravel-cuid2
Latest Version:v3.0.1
PHP:^8.2
Laravel:12 · 13
License:MIT
Last Updated:Aug 1, 2026
Links: GitHub  ·  Packagist
Maintainer: mcandylab

Laravel CUID2

🇬🇧 English | 🇷🇺 Русский

Latest Version on Packagist Total Downloads run-tests

Use CUID2 as primary keys for your Eloquent models in Laravel. The package provides a model trait, a global cuid2() helper and schema macros for migrations. Generation is delegated to the visus/cuid2 library.

Requirements

  • PHP >= 8.2
  • Laravel 12 or 13 (Laravel 13 requires PHP 8.3+)

Installation

composer require mcandylab/laravel-cuid2

The package uses auto-discovery. Publish the config if needed:

php artisan vendor:publish --provider="Mcandylab\LaravelCuid2\LaravelCuid2ServiceProvider" --tag="config"

Usage

Model trait

Add the HasCuid2 trait — the primary key will be automatically populated with a valid CUID2 when a record is created:

use Illuminate\Database\Eloquent\Model;
use Mcandylab\LaravelCuid2\Concerns\HasCuid2;

class Post extends Model
{
    use HasCuid2;
}

The trait sets keyType = 'string' and incrementing = false for you. To generate a cuid2 for more than just the primary key, override uniqueIds():

public function uniqueIds(): array
{
    return [$this->getKeyName(), 'public_id'];
}

Migrations

The cuid2() and foreignCuid2() macros declare varchar columns (Laravel's Schema::defaultStringLength, 255 by default). They are fully indexable across databases and fit any value the package generates, including prefixed {prefix}_{cuid2} identifiers:

Schema::create('posts', function (Blueprint $table) {
    $table->cuid2()->primary();      // id column
    $table->string('title');
    $table->timestamps();
});

Schema::create('comments', function (Blueprint $table) {
    $table->cuid2()->primary();
    $table->foreignCuid2('post_id')->constrained();
    $table->text('body');
});

For polymorphic relations use cuid2Morphs() (and nullableCuid2Morphs()), the CUID2 counterparts of Laravel's ulidMorphs(). They add a {name}_type string column, a {name}_id varchar column and a composite index:

Schema::create('tokens', function (Blueprint $table) {
    $table->cuid2()->primary();
    $table->cuid2Morphs('tokenable');          // tokenable_type + tokenable_id
    $table->string('token');
});

// nullable variant
$table->nullableCuid2Morphs('tokenable');

Helper

$id = cuid2();      // 24 characters (or config('laravel-cuid2.length'))
$short = cuid2(10); // exactly 10 characters (4..32 allowed)

Facade

use Mcandylab\LaravelCuid2\LaravelCuid2Facade as Cuid2;

Cuid2::generate();          // generate an id
Cuid2::isValid($someId);    // validate a string

Str macros

Aligned with the core Str::uuid() / Str::ulid() helpers:

use Illuminate\Support\Str;

Str::cuid2();          // generate (respects config('laravel-cuid2.length'))
Str::cuid2(10);        // exactly 10 characters (4..32 allowed)
Str::isCuid2($value);  // validate a value (false for non-strings)

Faker

A cuid2() Faker formatter is available for factories and seeders:

use App\Models\Post;

Post::factory()->create(['id' => fake()->cuid2()]);

fake()->cuid2();   // generate (respects config('laravel-cuid2.length'))
fake()->cuid2(10); // exactly 10 characters (4..32 allowed)

Validation

The cuid2 rule validates that a value is a well-formed CUID2 of any valid length (4..32). It is available in three forms:

use Illuminate\Validation\Rule;
use Mcandylab\LaravelCuid2\Rules\Cuid2;

$request->validate([
    'id'    => 'cuid2',          // string rule
    'ref'   => [new Cuid2],      // rule object
    'ext'   => [Rule::cuid2()],  // rule macro
]);

Prefixed identifiers (Stripe-style)

You can produce human-readable identifiers such as user_p6p168tx… by declaring a $cuid2Prefix property on the model. The prefix is just an envelope — the cuid2 part after the _ stays fully spec-compliant:

class User extends Model
{
    use HasCuid2;

    protected string $cuid2Prefix = 'user';
}

No special column type is needed: cuid2() and foreignCuid2() already create a varchar that fits any {prefix}_{cuid2} value:

Schema::create('users', function (Blueprint $table) {
    $table->cuid2()->primary();
    $table->string('name');
});

// a foreign key referencing a prefixed model
$table->foreignCuid2('user_id');

The generators accept an optional prefix too:

cuid2(prefix: 'user');            // user_…
Str::cuid2(prefix: 'user');       // user_…
fake()->cuid2(prefix: 'user');    // user_…

Validation can optionally check the prefix — it verifies the {prefix}_ and that the remainder is a valid CUID2. Without a prefix the rule is unchanged:

$request->validate([
    'id'  => 'cuid2:user',                 // string rule
    'id2' => [new Cuid2('user')],          // rule object
    'id3' => [Rule::cuid2(prefix: 'user')], // rule macro
]);

Str::isCuid2($value, 'user');              // and via the Str macro

Note: cuid2Morphs() / nullableCuid2Morphs() take no prefix argument — a polymorphic column may point at models with different prefixes, and the prefix is already implied by the {name}_type column. Like every id column in this package, {name}_id is a varchar, so a prefixed model can safely be a polymorphic target without truncation.

Configuration

config/laravel-cuid2.php:

return [
    // Identifier length (4..32). The cuid2 standard is 24.
    'length' => (int) env('CUID2_LENGTH', 24),
];

length only affects generation (cuid2(), Str::cuid2(), fake()->cuid2(), HasCuid2). Schema macros are unaffected — they always declare a varchar, so changing the length does not require a migration.

Testing

composer test

Changelog

See CHANGELOG.

Contributing

See CONTRIBUTING.

Security

If you discover any security related issues, please open an issue.

Credits

License

The MIT License (MIT). See License File.

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

670,032,257 9,703
laravel/framework

The Laravel Framework.

640,572,096 35,338
laravel/tinker

Powerful REPL for the Laravel framework.

546,909,908 7,440
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

465,816,364 607
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

431,187,642 4,660

Version History

Version Released PHP Laravel License
v3.0.1 ^8.2 ^12.0 || ^13.0 MIT
v3.0.0 ^8.2 ^12.0 || ^13.0 MIT
v2.0.0 ^8.2 ^12.0 || ^13.0 MIT
v1.2.0 ^8.2 ^12.0 || ^13.0 MIT
v1.1.0 ^8.2 ^12.0 || ^13.0 MIT