Skip to content

veelasky/laravel-hashid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Laravel HashId

CI/CD Pipeline πŸ”’ Security Scanning Codacy Badge codecov Latest Stable Version StyleCI Total Downloads Dependents License

Automatic HashId generator for your Laravel Eloquent models.

About

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.

✨ Latest Features

Version 3.2.0 introduces powerful column selection capabilities along with full Laravel 11/12 and PHP 8.4 compatibility.

πŸ”₯ Column Selection API (New in v3.2.0)

// 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

Compatibility

Modern Laravel Support (Recommended)

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

Full Version Matrix

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.0 or 3.1
  • Laravel 10+ β†’ Use 3.2 (stable) or 4.x (development)
  • Latest features β†’ Use 3.2+ with column selection support

Installation

composer require veelasky/laravel-hashid

With Laravel's package auto-discovery, the package will be automatically registered.

Quick Start

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;
}

Usage Examples

Basic Usage

$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();

Column Selection (New in v3.2)

// 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']);

Persisting HashId to Database

class User extends Model
{
    use HashableId;

    protected $shouldHashPersist = true;  // Persist hash to database
    protected $hashColumnName = 'hashid';  // Custom column name (optional)
}

Route Model Binding

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
    }
}

Validation Rules

use App\Models\User;
use Veelasky\LaravelHashId\Rules\ExistsByHash;

$request->validate([
    'user_id' => ['required', new ExistsByHash(User::class)],
]);

Advanced Usage

Repository Pattern Access

// 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');

Shared Hash Across Models

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; // true

Configuration

You can configure HashId behavior using environment variables:

HASHID_SALT=your-custom-salt
HASHID_LENGTH=10
HASHID_ALPHABET=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890

Or publish the configuration file:

php artisan vendor:publish --tag=laravel-hashid-config

License

MIT License. Feel free to use this package in your projects!

About

HashId Implementation on Laravel Eloquent ORM

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors 12

Languages