PHPackages                             steven-fox/laravel-sqids - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. steven-fox/laravel-sqids

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

steven-fox/laravel-sqids
========================

Sqids for Laravel.

v0.2.0(2y ago)51.2k—0%[2 PRs](https://github.com/steven-fox/laravel-sqids/pulls)MITPHPPHP ^8.2

Since Oct 28Pushed 1y ago1 watchersCompare

[ Source](https://github.com/steven-fox/laravel-sqids)[ Packagist](https://packagist.org/packages/steven-fox/laravel-sqids)[ Docs](https://github.com/steven-fox/laravel-sqids)[ RSS](/packages/steven-fox-laravel-sqids/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (13)Versions (5)Used By (0)

Sqids for Laravel.
==================

[](#sqids-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/38cbde191256262492c31f4055b9d1ed3e98c05a9dc2ccb821e351acfc5212a8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73746576656e2d666f782f6c61726176656c2d73716964732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/steven-fox/laravel-sqids)[![GitHub Tests Action Status](https://camo.githubusercontent.com/c0e9f0ec9def62832d1d436d106fc47c1f45eb21b977d601003cefa2b3f3bd95/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f73746576656e2d666f782f6c61726176656c2d73716964732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/steven-fox/laravel-sqids/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/ebaf79bb9227e7475b6fe49a48c7ca31bfca71d1d13f2e79b94bec00e077c3c8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f73746576656e2d666f782f6c61726176656c2d73716964732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/steven-fox/laravel-sqids/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/4ece528cf4c5e92cefbfd49695738fab884a5db403eda485552d7ab7075e99a3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73746576656e2d666f782f6c61726176656c2d73716964732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/steven-fox/laravel-sqids)

This package brings Sqids functionality, with the option of multiple named configurations (alphabet and minimum encoding length), to your Laravel application. It's a batteries-included wrapper for the PHP Sqids package.

### Features

[](#features)

- Uses the PHP Sqids implementation under the hood, so features like custom alphabets, minimum encoded sqid lengths, and blocklists are available with this package.
- Permits multiple sqid configurations that can be referenced by name to use for decoding/encoding with a specific alphabet and minLength.
- Makes it easy to validate an encoded Sqid to ensure it's canonical (in other words, to ensure it is an original Sqid that was generated with your alphabet configuration) when decoding.
- Makes it easy to work with single integers for the common use case of encoding/decoding numeric database IDs.
- Makes it easy to extend the package's EncodedSqid &amp; DecodedSqid objects so you can create dedicated Sqid classes for your custom configurations (meaning you can have a set of Sqids that are always used for a particular Model or other use case).

Here are some quick examples.

```
$idFromDatabase = 7391;

$decodedSqid = \StevenFox\LaravelSqids\Sqids\DecodedSqid::new($idFromDatabase);
$encodedSqid = $decodedSqid->encode(); // instance of EncodedSqid

echo $encodedSqid->id(); // '2h2L'

$decodeAgain = $encodedSqid->decode(); // instance of DecodedSqid

echo $decodeAgain->numbers(); // [7391]
echo $decodeAgain->toInt(); // 7391
```

```
$sqidFromRequest = '2h2L';

$encodedSqid = \StevenFox\LaravelSqids\Sqids\EncodedSqid::new($sqidFromRequest);
$decodedSqid = $encodedSqid->decode(); // instance of DecodedSqid

echo $decodedSqid->numbers(); // [7391]
echo $decodedSqid->toInt(); // 7391
```

```
\StevenFox\LaravelSqids\Facades\Sqidder::encode([7391]); // '2h2L'
\StevenFox\LaravelSqids\Facades\Sqidder::decode('2h2L'); // [7391]
```

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require steven-fox/laravel-sqids
```

You can publish the config file with:

```
php artisan vendor:publish --tag="laravel-sqids-config"
```

Once the config file has been published, it is recommended to set your custom sqid configurations. This package allows you to use one or more named sqid configurations simultaneously and define one of them as a default. For each sqid configuration, you may specify a custom alphabet (which can provide a sense of uniqueness to your encoded sqids) and a minimum sqid length.

```
// /config/sqids.php

return [
    // Specify the default Sqid configuration name.
    'default' => 'primary',

    // Specify one or more Sqid configurations that can be used as needed.
    'sqids' => [

        // This Sqid configuration will have a name of 'primary'.
        'primary' => [
            // Randomize IDs by specifying a custom/shuffled alphabet.
            'alphabet' => 'MGZAJbNxVrhm5Sz47URHwXQf1FPgvlc2ptjY9uLEianODyKCosBIkd0q3W6eT8',

            // Enforce a minimum length for the encoded Sqid.
            'minLength' => 0,
        ],

        // This Sqid configuration has a name of 'other'.
        'other' => [
            'alphabet' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
            'minLength' => 0,
        ],

        // Perhaps this Sqid configuration is used for encoding/decoding User records.
        'user' => [
            'alphabet' => 'kQZlcRHnxL4GY6JW3Ir1weDK8MbOBztmqg7d02jp9UXyNTiSa5FEsvVPhAuofC',
            'minLength' => 0,
        ],
    ],

    // Prevent specific words from appearing anywhere in the encoded Sqids.
    'blocklist' => [
        '0rgasm',
        '1d10t',
        '1d1ot',
        ...
    ],
];
```

Usage
-----

[](#usage)

### Prerequisite: Review the Sqids documentation

[](#prerequisite-review-the-sqids-documentation)

Although this package is easy to use, it's recommended to review the [Sqids documentation](https://sqids.org/) to understand the underlying functionality. There are a number of [good use cases for Sqids](https://sqids.org/faq#recommended), but there are also many where [they should **not** be used](https://sqids.org/faq#not-recommended) (for example, as encryption or to encode sensitive numbers).

Consider using the [playground](https://sqids.org/playground) to get a feel for the encoded Sqids created by different alphabet + minLength combinations.

### The Intended Pattern

[](#the-intended-pattern)

It's intended to primarily use extensions of the `EncodedSqid` and `DecodedSqid` classes in your application. For example:

- You create a `DecodedSqid` using the numeric `id` of a model and then you `encode()` that, ultimately providing a string you can use somewhere (like in a url).
- When your app handles something that involves a Sqid string (like a request url), you create an `EncodedSqid` instance using the provided Sqid string and then you `decode()`, ultimately yielding the numeric model `id` you need to fetch from the database.

If you plan on using multiple Sqid configurations (for example, one configuration per model), then you will likely want to create your own Encoded/Decoded Sqid classes - one for each configuration - that extend the `EncodedSqid` and `DecodedSqid` classes provided by this package. By doing this, you can instantiate your objects with the correct Sqid configuration "baked in", meaning you will not have to call the `$encodedSqid->usingConfigName('user-model')->...` method everywhere.

Here's a simple example:

```
use StevenFox\LaravelSqids\Sqids\DecodedSqid;
use StevenFox\LaravelSqids\Sqids\EncodedSqid;

class UserDecodedSqid extends DecodedSqid
{
    public const CONFIG_NAME = 'user';
    public const ENCODED_SQID_CLASS = UserEncodedSqid::class;
}
```

With that in place, you can now instantiate your `UserDecodedSqid` and the `'user'` configuration will be used automatically. Once you perform an `encode()` operation, the resulting object will be an instance of your `UserEncodedSqid`, which will also reference the `'user'` configuration automatically.

```
$sqid = UserDecodedSqid::new($userId)->encode(); // instance of UserEncodedSqid
```

Making a custom `EncodedSqid` is essentially the same process.

### Using EncodedSqids

[](#using-encodedsqids)

### Using the DecodedSqid

[](#using-the-decodedsqid)

### Using the Sqidder

[](#using-the-sqidder)

In this package, the `Sqidder` (pronounced squid-er) is a Sqids implementation class that can perform encoding and decoding based on a Sqid configuration. At its core, it is a wrapper for the `SqidsInterface` implementation. Thus, it provides the `encode()` and `decode()` interface methods, along with an additional method from the `ConfigBasedSqidder` contract (original to this package) that permit selection of a specific `coder` for a particular Sqid configuration.

The `EncodedSqid` and `DecodedSqid` classes use the `Sqidder` internally and that is meant to be the primary interaction. However, it is also possible to use the `Sqidder` directly and a Facade is provided for easy access.

```
use StevenFox\LaravelSqids\Facades\Sqidder

// When a specific coder isn't specified,
// the coder for the default configuration will be used.
// The /config/sqids.php file above specifies the configuration
// named 'primary' should be used as the default.
Sqidder::encode([1]); // 'Ko'
Sqidder::encode([1, 2, 3]); // 'xjECV2'

Sqidder::decode('Ko'); // [1]
Sqidder::decode('xjECV2'); // [1, 2, 3]

// Specifying a config by name.
// The configuration *must* exist in the sqids config file.
// A \StevenFox\LaravelSqids\Exceptions\NamedSqidConfigurationNotFoundException will be thrown otherwise.
Sqidder::forConfig('other'); // A concrete SqidsInterface instance for the 'other' configuration.
Sqidder::forConfig('other')->encode([1]); // 'Uk'
Sqidder::forConfig('other')->decode('Uk'); // [1]

Sqidder::forConfig('does-not-exist'); // exception thrown
```

#### Canonical Sqids

[](#canonical-sqids)

The `Sqidder` does **not** perform Sqid validation when decoding to check if the encoded string is canonical. The `EncodedSqid`, however, does provide a validation option and is enabled by default when decoding.

> Due to the Sqids algorithm, multiple encoded strings may decode to the same number(s). Thus, a *canonical* Sqid is one that originally came from your alphabet configuration. You can check if an encoded Sqid is canonical by decoding it into its number(s) and then re-encoding those numbers. If the original Sqid and the re-encoded Sqid are identical, then the original is canonical.
>
> For example, using a standard alphabet, the encoded string 'Ul' will decode to the number \[41\]. However, re-encoding the number \[41\] produces a Sqid of 'qp'. Thus, the original Sqid of 'Ul' is not canonical and, in many circumstances, should be considered invalid.

### Using Multiple Sqid Configurations

[](#using-multiple-sqid-configurations)

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Steven Fox](https://github.com/steven-fox)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance27

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 74.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~193 days

Total

2

Last Release

734d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c3c9b257c10b41dc9dcad9d29657e430ea0b902c91a0e9392fa9f57bf48bd349?d=identicon)[stevenfox](/maintainers/stevenfox)

---

Top Contributors

[![steven-fox](https://avatars.githubusercontent.com/u/62109327?v=4)](https://github.com/steven-fox "steven-fox (23 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (5 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (3 commits)")

---

Tags

laravelhashidshashidsqidslaravel-sqidssteven-foxsqid

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/steven-fox-laravel-sqids/health.svg)

```
[![Health](https://phpackages.com/badges/steven-fox-laravel-sqids/health.svg)](https://phpackages.com/packages/steven-fox-laravel-sqids)
```

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.7k28.9M627](/packages/spatie-laravel-data)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[red-explosion/laravel-sqids

Easily generate Stripe/YouTube looking IDs for your Laravel models.

4530.8k](/packages/red-explosion-laravel-sqids)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
