Search by

grantholle / scru64-laravel

grantholle

Use SCRU64 ID's in your Laravel application.

Package info

github.com/grantholle/scru64-laravel

pkg:composer/grantholle/scru64-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:04 UTC

This package is auto-updated.

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


README

Latest Version on Packagist Tests Total Downloads

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

SCRU64 IDs are 12-character, case-insensitive, time-sortable strings (0u375nxqh5cq) that also fit in a signed BIGINT.

Installation

composer require grantholle/scru64-laravel

It works out of the box on a single server. If more than one server generates IDs, give each one a unique node ID in its .env, or uniqueness is not guaranteed:

SCRU64_NODE_SPEC=42/8

The format is <node_id>/<node_id_size>; see the scru64 README for details. You can also publish the config file:

php artisan vendor:publish --tag="scru64-laravel-config"

Usage

use GrantHolle\Scru64Laravel\Concerns\HasScru64Ids;
use Illuminate\Database\Eloquent\Model;

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

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

scru64($column = 'id') and foreignScru64($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->scru64()->charset('ascii')->collation('ascii_bin');
$table->foreignScru64('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.