Laravel adapter for sqids-php.
Generate short, unique, non-sequential IDs for your models, routes, validation, and more.
- Generate short, unique, non-sequential IDs
- Easy helpers:
sqid($id)/unsqid($hash) - Facade:
Sqids::encode()/decode() - Validation rule:
sqid - Fully configurable alphabet, min length, and blocklist
composer require a-sabagh/laravel-sqidsThe Sqids service allows you to convert integers into short, URL-safe strings and decode them back.
use ASabagh\LaravelSqids\Facades\Sqids;
$id = 123;
$encodeString = Sqids::encode([$id]); // e.g. "Lqj8a0"
$decodeNumber = Sqids::decode($encodeString); // [123]$id = 456;
$encodeString = Sqids::encodeInteger($id);
$decodeInteger = Sqids::decodeInteger($encodeString);$decodeCollection = Sqids::decodeCollect($encodeString);Notes:
-
encodealways expects an array of integers. -
decodereturns an array. -
encodeInteger/decodeIntegerwork with single integers. -
decodeCollectreturns aCollectionfor easier chaining with Laravel collections.
Laravel Sqids allows you to customize its behavior via a configuration file. Publishing the configuration lets you modify settings such as the alphabet, minimum length, and blocklist.
Use the following Artisan command to publish the configuration to your application:
php artisan vendor:publish --tag=sqidsHere are each of the drivers setup for your application. Example configuration has been included, but you may add as many drivers as you would like.
'drivers' => [
'default' => [
'pad' => env('SQIDS_DEFAULT_PAD', ''),
'length' => env('SQIDS_DEFAULT_LENGTH', 6),
'blocklist' => env('SQIDS_DEFAULT_BLOCK_LIST', []),
'alphabet' => env('SQIDS_DEFAULT_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'),
],
]Laravel Sqids allows you to publish the configuration and define multiple drivers, each with its own settings.
The configuration file includes a drivers array. You can configure one or more drivers for different encoding strategies.
You can add additional drivers with their own configuration:
'drivers' => [
'default' => [ /* default config */ ],
'short_ids' => [
'pad' => '0',
'length' => 4,
'blocklist' => ['0', 'O', 'I', '1'],
'alphabet' => 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789',
],
],Then, you can use a specific driver when encoding/decoding:
$encoded = Sqids::driver('short_ids')->encode([$id]);
$decoded = Sqids::driver('short_ids')->decode($encoded);Once a driver has been registered and configured in your config/sqids.php, you can access it directly using a camelCase method on the Sqids Facade.
$shortId = Sqids::shortIds()->encode([$id]);
$defaultId = Sqids::default()->encode([$id]);$isValid = Sqids::encodedStringValid($randomString);Laravel Sqids provides a custom validation rule to ensure that a given input is a valid Sqid string. You can use it in form requests, inline validation, and with custom drivers.
$encoded = Sqids::encodeInteger($id);
$validator = Validator::make(
['endpoint' => $encoded],
['endpoint' => [new SqidsValidationRule]]
);
if ($validator->passes()) {
// Valid Sqid
}| Function |
|---|
sqids(array $numbers, ?string $driver = null): string |
unsqids(string $encodedString, ?string $driver = null): array |
sqidsInt(int $id, ?string $driver = null): int |
unsqidsInt(string $encodedString, ?string $driver = null): int |
unsqidsCollect(string $encodedString, ?string $driver = null): Collection |
// Encode multiple numbers
$encoded = sqids([1, 2, 3]); // e.g. "Lqj8a0"
// Decode back
$decoded = unsqids($encoded); // [1, 2, 3]
// Encode a single integer
$singleEncoded = sqidsInt(123); // e.g. "M7k2b1"
// Decode single integer
$singleDecoded = unsqidsInt($singleEncoded); // 123
// Decode into a Collection
$collection = unsqidsCollect($encoded); // Collection([1, 2, 3])