Automatic HashId generator for your Laravel Eloquent models.
Laravel HashId provides an elegant way to add hashed IDs to your Eloquent models. It generates unique, non-sequential hashes for your model IDs and provides convenient methods to work with them.
Version 3.2.3 introduces automatic route key generation, eliminating boilerplate code, along with enhanced secure route model binding and powerful column selection capabilities for full Laravel 11/12 and PHP 8.4 compatibility.
Zero boilerplate: The HashableId trait now automatically provides getRouteKey() method:
class User extends Model
{
use HashableId;
// getRouteKey() automatically returns hash - no implementation needed!
}
route('users.show', $user); // Automatically generates: /users/k1jTdv6lSecurity improvement: Route model binding now only accepts valid hash values by default, preventing predictable ID enumeration attacks:
class User extends Model
{
use HashableId;
// Default: only hash resolution, numeric IDs return 404
}
// β
Secure: /users/k1jTdv6l works
// β Blocked: /users/1 returns 404 (prevents ID enumeration)// Get user by hash with specific columns (better performance!)
$user = User::byHash($hash, ['name', 'email']);
// Get user by hash with single column
$user = User::byHash($hash, ['name']);
// Column selection with exception handling
$user = User::byHashOrFail($hash, ['name', 'email']);Benefits:
- π Better Performance - Load only the columns you need
- π Type Safety - Automatic primary key inclusion when required
- π Backward Compatible - All existing code works unchanged
- π― Smart Defaults -
['*']loads all columns, just like before - π‘οΈ Enhanced Security - Prevents ID enumeration attacks by default
- β
Zero Boilerplate - No manual
getRouteKey()implementation needed
| Laravel HashId | PHP Version | Laravel 10 | Laravel 11 | Laravel 12 |
|---|---|---|---|---|
| 3.2 π | β₯ 8.1 | β | β | β |
| 4.x π | β₯ 8.1 | β | β | β |
- π Stable Release (3.2) - Recommended for production
- π Development Branch (4.x) - Latest improvements
| Laravel HashId | PHP Version | Laravel 6 | Laravel 7 | Laravel 8 | Laravel 9 | Laravel 10 | Laravel 11 | Laravel 12 |
|---|---|---|---|---|---|---|---|---|
| 1.x | β₯ 7.0 |
β | β | β | β | β | β | β |
| 2.x | β₯ 7.2 |
β | β | β | β | β | β | β |
| 3.0 | β₯ 7.4 |
β | β | β | β | β | β | β |
| 3.1 | β₯ 8.0 |
β | β | β | β | β | β | β |
| 3.2 π | β₯ 8.1 |
β | β | β | β | β | β | β |
| 4.x π | β₯ 8.1 |
β | β | β | β | β | β | β |
π Version Recommendations:
- Laravel 6-9 β Use
3.0or3.1 - Laravel 10+ β Use
3.2(stable) or4.x(development) - Latest features β Use
3.2+with column selection support
composer require veelasky/laravel-hashidWith Laravel's package auto-discovery, the package will be automatically registered.
Simply add the HashableId trait to any Eloquent model you want to use with HashId:
use Illuminate\Database\Eloquent\Model;
use Veelasky\LaravelHashId\Eloquent\HashableId;
class User extends Model
{
use HashableId;
}$user = User::find(1); // Find user by ID
$user->hash; // Get HashId automatically
// Find by HashId
$user = User::byHash($hash);
$user = User::byHashOrFail($hash); // Throws exception if not found
// Convert between ID and HashId
$hashedId = User::idToHash($id);
$originalId = User::hashToId($hash);
// Query scope
User::query()->byHash($hash)->get();// Load only specific columns for better performance
$user = User::byHash($hash, ['name', 'email']);
// Single column selection
$user = User::byHash($hash, ['name']);
// Column selection with exception handling
$user = User::byHashOrFail($hash, ['name', 'email']);class User extends Model
{
use HashableId;
protected $shouldHashPersist = true; // Persist hash to database
protected $hashColumnName = 'hashid'; // Custom column name (optional)
}The trait automatically overwrites route methods to use HashId:
Route::get('/users/{user}', [UserController::class, 'show']);
class UserController
{
public function show(User $user)
{
// $user resolves automatically by HashId
// Example URL: /users/k1jTdv6l
// Numeric IDs like /users/1 will return 404 by default (secure!)
}
}By default, route model binding only accepts valid hash values and returns 404 for plain numeric IDs, preventing predictable ID enumeration attacks:
class User extends Model
{
use HashableId;
// $bindingFallback = false; // Default behavior - only hash resolution
}
// β
This works: /users/k1jTdv6l
// β This returns 404: /users/1 (prevents ID enumeration)If you need to support both hash and numeric ID resolution (not recommended for production), you can enable the fallback:
class User extends Model
{
use HashableId;
protected $bindingFallback = true; // Allow both hash and numeric ID resolution
}
// β
Both work: /users/k1jTdv6l AND /users/1Custom field binding always uses Laravel's default behavior:
Route::get('/users/{user:slug}', [UserController::class, 'show']);
// This will resolve by 'slug' field, not by hashThe HashableId trait now automatically provides getRouteKey() method, so you don't need to implement it manually:
class User extends Model
{
use HashableId;
// No manual getRouteKey() implementation needed!
}
// Route generation now automatically uses hash
route('users.show', $user); // Generates: /users/k1jTdv6lBefore this version:
class User extends Model
{
use HashableId;
// Manual implementation was required
public function getRouteKey() {
return $this->hash;
}
}Benefits:
- β
Zero Boilerplate - No manual
getRouteKey()implementation needed - π Automatic - Works out of the box with the trait
- π‘οΈ Consistent - Uses the same hash values as route resolution
- π§ Configurable - Respects custom hash column names
- β¬ οΈ Backward Compatible - Existing custom implementations still work
use App\Models\User;
use Veelasky\LaravelHashId\Rules\ExistsByHash;
$request->validate([
'user_id' => ['required', new ExistsByHash(User::class)],
]);// Using the HashId facade
$hashedId = HashId::idToHash($id, User::class);
$originalId = HashId::hashToId($hash, User::class);
// Manual hash ID creation
$hashId = HashId::make('custom-key', 'custom-salt');class User extends Model
{
protected $hashKey = 'shared-hash-key';
}
class Customer extends User { }
$customer = Customer::find(1);
$user = User::find(1);
// Both will have the same hash
echo $customer->hash === $user->hash; // trueYou can configure HashId behavior using environment variables:
HASHID_SALT=your-custom-salt
HASHID_LENGTH=10
HASHID_ALPHABET=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890Or publish the configuration file:
php artisan vendor:publish --tag=laravel-hashid-configMIT License. Feel free to use this package in your projects!