grantholle / scru64-laravel
Use SCRU64 ID's in your Laravel application.
Fund package maintenance!
Requires
- php: ^8.4
- grantholle/scru64: ^1.0
- illuminate/database: ^11.0||^12.0||^13.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^11.0.0||^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
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.