bretterer/laravel-hashid

Generate and use HashIds for your Laravel models, just like native ULIDs and UUIDs
4,235 1
Install
composer require bretterer/laravel-hashid
Latest Version:v0.3.1
PHP:^8.3
Laravel:11 · 12 · 13
License:MIT
Last Updated:Jun 22, 2026
Links: GitHub  ·  Packagist
Maintainer: bretterer

Laravel Hashid

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.

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

644,549,754 9,698
laravel/framework

The Laravel Framework.

593,882,149 34,959
laravel/tinker

Powerful REPL for the Laravel framework.

507,193,856 7,441
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

423,477,751 607
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

399,241,428 4,660

Version History

Version Released PHP Laravel License
v0.3.1 ^8.3 ^11.0||^12.8||^13.0 MIT
v0.3.0 ^8.3 ^11.0||^12.8||^13.0 MIT
v0.2.0 ^8.3 ^11.0||^12.8||^13.0 MIT
v0.1.2 ^8.3 ^11.0||^12.8 MIT
v0.1.1 ^8.3 ^11.0||^12.8 MIT