iridiumintel/laravel-hashable-routes

A Laravel package that provides short, secure, and per-model configurable hashed route keys instead of incremental IDs.

Installs: 12

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Forks: 0

pkg:composer/iridiumintel/laravel-hashable-routes

v0.1.1 2025-09-27 14:49 UTC

This package is not auto-updated.

Last update: 2025-10-11 22:35:22 UTC


README

Latest Version on Packagist Total Downloads

A Laravel package that replaces incremental IDs with short, secure, and per-model configurable hash IDs.
Built on top of hashids/hashids.

๐Ÿš€ Features

  • Generate short, obfuscated IDs for your models (abc123 instead of 42).
  • Per-model configuration (custom salt & length).
  • Optional automatic route key override (so /users/abc123 instead of /users/42).
  • Helpers: encodeId(), decodeHash(), findByHashOrNull().
  • Artisan commands for direct encode/decode in terminal.
  • Plug-and-play with Laravel (composer require).

๐Ÿ“ฆ Installation

composer require iridiumintel/laravel-hashable-routes

Publish the config file:

php artisan vendor:publish --tag=config

โš™๏ธ Configuration

config/hashable.php

return [
    'default' => [
        'salt' => env('HASHABLE_SALT', env('APP_KEY')),
        'length' => env('HASHABLE_LENGTH', 10),
        'override_route_key' => env('HASHABLE_ROUTE_KEY', true),
        'strict_binding' => env('HASHABLE_STRICT', true),
    ],

    'models' => [
        // Example: per-model override
        App\Models\User::class => [
            'salt' => env('HASHABLE_USER_SALT', env('APP_KEY')),
            'length' => 8,
            'override_route_key' => true,
            'strict_binding' => true,
        ],
    ],
];

๐Ÿ”‘ Usage

Add the trait to your Eloquent model:

use IridiumIntel\Hashable\Hashable;

class User extends Model
{
    use Hashable;
}

Now:

$user = User::find(42);

echo $user->hash_id; 
// e.g. "gB9xL0Qjz"

$found = User::findByHash("gB9xL0Qjz");
// returns the same user

$maybe = User::findByHashOrNull("invalid");
// returns null instead of throwing

Route binding

If override_route_key is enabled (default):

Route::get('/users/{user}', fn(User $user) => $user);

// /users/gB9xL0Qjz โ†’ User#42

If disabled, routes continue using the standard id, but you can still use findByHash() manually.

๐Ÿงช Artisan Commands

Encode an ID:

php artisan hashable:encode "App\Models\User" 42
# Output: Hash for App\Models\User #42: gB9xL0Qjz

Decode a hash:

php artisan hashable:decode "App\Models\User" gB9xL0Qjz
# Output: Decoded ID for App\Models\User (gB9xL0Qjz): 42

๐Ÿ“œ License

The MIT License (MIT).
See LICENSE for details.