bretterer / laravel-hashid
Generate and use HashIds for your Laravel models, just like native ULIDs and UUIDs
Fund package maintenance!
bretterer
Installs: 5
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/bretterer/laravel-hashid
Requires
- php: ^8.3
- illuminate/contracts: ^11.0||^12.8
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^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
README
Generate and use HashIds for your Laravel models, just like native ULIDs and UUIDs.
This package allows you to generate and use HashIds for your Laravel models, just like native ULIDs and UUIDs. It is easy to integrate and works seamlessly with Laravel's Eloquent models.
Installation
Install via composer:
composer require bretterer/laravel-hashid
Usage
1. Add the Trait to Your Model
use Bretterer\LaravelHashId\Traits\HasHashIds; class User extends Model { use HasHashIds; // ... }
2. Use HashId Columns in Migrations
Schema::create('users', function ($table) { $table->hashId('id', 16)->primary(); $table->string('name'); }); Schema::create('posts', function ($table) { $table->hashId('id', 16)->primary(); $table->foreignHashId('user_id', 'users', 'id', 16); $table->string('title'); });
3. Creating Models
$user = User::create(['name' => 'Alice']); echo $user->id; // 16-character base62 HashId $post = Post::create(['user_id' => $user->id, 'title' => 'Hello']);
4. Custom Prefixes
class PrefixedUser extends Model { use HasHashIds; public function idPrefix(): string { return 'usr'; } } $user = PrefixedUser::create(['name' => 'Dave']); echo $user->id; // usr_XXXXXXXXXXXXXXX
5. HashId Generator
use Bretterer\LaravelHashId\LaravelHashId; $generator = new LaravelHashId(); $hashId = $generator->generate(16); // Random HashId $hashIdFromValue = $generator->generateFromValue('value', 'salt', 16); // HashId from value
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Brian Retterer
- All Contributors
- Special thanks to Spatie for their excellent Laravel package skeleton project.
License
The MIT License (MIT). Please see License File for more information.