Search by

grantholle / scru128-laravel

grantholle

Use SCRU128 ID's in your Laravel application.

Package info

github.com/grantholle/scru128-laravel

pkg:composer/grantholle/scru128-laravel

Fund package maintenance!

Grant Holle

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.1 2026-09-17 05:03 UTC

This package is auto-updated.

Last update: 2026-09-17 05:06:41 UTC


README

Latest Version on Packagist Tests Total Downloads

Use SCRU128 identifiers as Eloquent primary keys, the same way you'd use Laravel's HasUuids. Built on grantholle/scru128.

SCRU128 IDs are 25-character, case-insensitive, time-sortable strings (0372ijojuxuhjsfkeryi2mrtm) with 128 bits of entropy.

Installation

composer require grantholle/scru128-laravel

No configuration needed.

Usage

use GrantHolle\Scru128Laravel\Concerns\HasScru128Ids;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasScru128Ids;
}
Schema::create('posts', function (Blueprint $table) {
    $table->scru128(); // char('id', 25)->primary()
    // ...
});

Schema::create('comments', function (Blueprint $table) {
    $table->scru128();
    $table->foreignScru128('post_id')->constrained();
});

scru128($column = 'id') and foreignScru128($column) are Blueprint macros; the foreign variant behaves like foreignUuid().

On MySQL/MariaDB, IDs are lowercase base36, so an ASCII binary collation keeps the column and its indexes compact and makes comparisons cheaper:

$table->scru128()->charset('ascii')->collation('ascii_bin');
$table->foreignScru128('post_id')->charset('ascii')->collation('ascii_bin')->constrained();

The trait sets $incrementing = false and $keyType = 'string', fills the key on create, and makes route model binding 404 on malformed IDs, exactly like HasUuids. Override uniqueIds() to generate IDs for additional columns:

public function uniqueIds(): array
{
    return ['id', 'public_id'];
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.