A Laravel package for encoding and decoding database IDs using Sqids (next-generation Hashids). This package provides a clean way to obfuscate your database IDs in URLs and API responses while maintaining Laravel conventions.
- π’ Encode/decode database IDs using Sqids (next-gen Hashids)
- π‘οΈ Built-in profanity filter with customizable blocklist
- π Eloquent model trait for automatic ID handling
- π£οΈ Route model binding support with encoded IDs
- βοΈ Signed IDs with expiration support
- π§ Helper functions for easy usage
- βοΈ Configurable alphabet, length, and blocklist
- π Laravel 10, 11, and 12 support
- π§ͺ Comprehensive test suite with Pest
- π Full static analysis with PHPStan
- β‘ Better performance than traditional Hashids
- PHP 8.2+
- Laravel 10.x, 11.x, or 12.x
- BCMath or GMP extension (automatically handled by Sqids)
Install the package via Composer:
composer require litepie/hashidsThe service provider will be automatically registered. To publish the config file:
php artisan vendor:publish --provider="Litepie\Hashids\HashidsServiceProvider" --tag="hashids-config"The configuration file config/hashids.php allows you to customize:
return [
'alphabet' => env('HASHIDS_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'),
'length' => env('HASHIDS_LENGTH', 0),
'blocklist' => env('HASHIDS_BLOCKLIST') ? explode(',', env('HASHIDS_BLOCKLIST')) : null,
];You can also set these values in your .env file:
HASHIDS_ALPHABET=abcdefghijklmnopqrstuvwxyz1234567890
HASHIDS_LENGTH=10
HASHIDS_BLOCKLIST=word1,word2,word3// Encode an ID
$hash = hashids_encode(123); // returns something like "bM"
// Decode a hash
$id = hashids_decode('bM'); // returns 123
// Encode multiple values
$hash = hashids_encode([123, 456]); // encode multiple values
// Decode to array
$ids = hashids_decode('86Rf07'); // returns [123, 456]
// Using new Sqids functions (aliases)
$hash = sqids_encode(123); // same as hashids_encode
$id = sqids_decode('bM'); // same as hashids_decodeuse Litepie\Hashids\Facades\Hashids;
$hash = Hashids::encode([123]);
$id = Hashids::decode('bM');Add the trait to your Eloquent models:
use Litepie\Hashids\Traits\Hashids;
class User extends Model
{
use Hashids;
// Your model code...
}// Get encoded ID
$user = User::find(1);
echo $user->eid; // encoded ID attribute
echo $user->getRouteKey(); // for route model binding
// Find by encoded ID
$user = User::findOrFail('bM');
$user = User::findOrNew('bM');
// Signed IDs (with expiration)
$signedId = $user->getSignedId('+1 hour'); // expires in 1 hour
$signedId = $user->getSignedId(1234567890); // expires at timestamp
$signedId = $user->getSignedId(); // never expires
// Find by signed ID
$user = User::findBySignedId($signedId);The trait automatically supports Laravel's route model binding:
// routes/web.php
Route::get('/users/{user}', function (User $user) {
return $user;
});Now you can use encoded IDs in your URLs:
/users/bM instead of /users/1
- The alphabet is automatically shuffled for uniqueness
- Built-in profanity filter prevents offensive words in IDs
- Signed IDs include salt verification and optional expiration
- IDs are obfuscated but not encrypted (don't rely on them for security)
- Customizable blocklist for additional word filtering
This package uses Sqids instead of traditional Hashids, providing:
- β‘ Better Performance - More efficient algorithm
- π‘οΈ Built-in Profanity Filter - Automatic offensive word prevention
- ποΈ Custom Blocklist - Block specific words from appearing in IDs
- π§ Modern PHP - Optimized for PHP 8.2+
- π Active Development - Regular updates and improvements
composer testcomposer analysecomposer formatPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
This package is open-sourced software licensed under the MIT license.
- Issues: GitHub Issues
- Documentation: README
- Source: GitHub Repository
This package is part of the Litepie ecosystem, developed by Renfos Technologies.
- Vendor: Litepie
- Framework: Lavalite
- Company: Renfos Technologies
- π Website: https://lavalite.org
- π Documentation: https://docs.lavalite.org
- πΌ Company: https://renfos.com
- π§ Support: [email protected]
Built with β€οΈ by Renfos Technologies
Empowering developers with robust Laravel solutions