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

v0.1.2 2025-09-27 03:06 UTC

This package is auto-updated.

Last update: 2025-09-27 03:06:50 UTC


README

Generate and use HashIds for your Laravel models, just like native ULIDs and UUIDs.

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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

License

The MIT License (MIT). Please see License File for more information.