Looking to hire Laravel developers? Try LaraJobs

scru128-laravel maintained by grantholle

Description
Use SCRU128 ID's in your Laravel application.
Author
Last update
2026/09/17 07:03 (dev-main)
License
Downloads
2

Comments
comments powered by Disqus

SCRU128 IDs for Laravel

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.