grantholle / scru128-laravel
Use SCRU128 ID's in your Laravel application.
Fund package maintenance!
Requires
- php: ^8.4
- grantholle/scru128: ^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 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.