PHPackages                             red-explosion/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. red-explosion/laravel-sqids

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

red-explosion/laravel-sqids
===========================

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

v2.1.0(2mo ago)4530.8k↑31.9%7[1 issues](https://github.com/red-explosion/laravel-sqids/issues)MITPHPPHP ^8.4CI passing

Since Nov 28Pushed 2mo ago3 watchersCompare

[ Source](https://github.com/red-explosion/laravel-sqids)[ Packagist](https://packagist.org/packages/red-explosion/laravel-sqids)[ Docs](https://github.com/red-explosion/laravel-sqids)[ RSS](/packages/red-explosion-laravel-sqids/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (10)Dependencies (20)Versions (17)Used By (0)

Laravel Sqids
=============

[](#laravel-sqids)

[![Latest Version on Packagist](https://camo.githubusercontent.com/8bf89f51f98c772626dc3ece94e3182b448a1ef47cac80a1dab627dc52a629f4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7265642d6578706c6f73696f6e2f6c61726176656c2d73716964732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/red-explosion/laravel-sqids)[![GitHub Tests Action Status](https://camo.githubusercontent.com/89d9658861da296745ebe054690b7eb69a44834eb9e47bca7d4bab818671386e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7265642d6578706c6f73696f6e2f6c61726176656c2d73716964732f74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/red-explosion/laravel-sqids/actions/workflows/tests.yml?query=branch:main)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/0570bcd01e5c2038675e61eb90205419397ac8bf9540dfbadaba4e8c3d48bdfb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7265642d6578706c6f73696f6e2f6c61726176656c2d73716964732f636f64696e672d7374616e64617264732e796d6c3f6c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/red-explosion/laravel-sqids/actions/workflows/coding-standards.yml?query=branch:main)[![Total Downloads](https://camo.githubusercontent.com/d43500c174803da0e4bdb92df1e8ec9e97db8ae355be0ebe54d4b773400b3bce/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7265642d6578706c6f73696f6e2f6c61726176656c2d73716964732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/red-explosion/laravel-sqids)

Laravel Sqids (pronounced "squids") allows you to easily generate Stripe/YouTube looking IDs for your Laravel models. These IDs are short and are guaranteed to be Collision free.

For more information on Sqids, we recommend checking out the official Sqids (formerly Hashids) website: .

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

[](#installation)

You can install the package via composer:

```
composer require red-explosion/laravel-sqids
```

You can publish the config file with:

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

Usage
-----

[](#usage)

### Using Sqids

[](#using-sqids)

To use Laravel Sqids, simply add the `RedExplosion\Sqids\Concerns\HasSqids` trait to your model:

```
use RedExplosion\Sqids\Concerns\HasSqids;

class User extends Authenticatable
{
    use HasSqids;
}
```

You will now be able to access the Sqid for the model, by calling the `sqid` attribute:

```
$user = User::first();

$sqid = $user->sqid; // usr_A3EyoEb2TO
```

The result of `$sqid` will be encoded value of the models primary key along with the model prefix.

Tip

Only integers can be encoded, and therefore we recommend using this package in conjunction with auto incrementing IDs.

If you would like to set a custom prefix for the model, you can override it by setting a `$sqidPrefix` property value on your model like so:

```
use RedExplosion\Sqids\Concerns\HasSqids;

class User extends Authenticatable
{
    use HasSqids;

    protected string $sqidPrefix = 'user';
}

$user = User::first();
$sqid = $user->sqid; // user_A3EyoEb2TO
```

### Builder Mixins

[](#builder-mixins)

Laravel Sqids provides a number of Eloquent builder mixins to make working with Sqids seamless.

#### Find by Sqid

[](#find-by-sqid)

To find a model by a given Sqid, you can use the `findBySqid` method:

```
$user = User::findBySqid('usr_A3EyoEb2TO');
```

If the model doesn't exist, `null` will be returned. However, if you would like to throw an exception, you can use the `findBySqidOrFail` method instead which will throw a `ModelNotFoundException` when a model can't be found:

```
$user = User::findBySqidOrFail('usr_invalid');
```

#### Where Sqid

[](#where-sqid)

To add a where clause to your query, you can use the `whereSqid` method:

```
$users = User::query()
    ->whereSqid('usr_A3EyoEb2TO')
    ->get();
```

This will retrieve all users where the Sqid/primary key matches the given value.

#### Where Sqid in

[](#where-sqid-in)

To get all models where the Sqid is in a given array, you can use the `whereSqidIn` method:

```
$users = User::query()
    ->whereSqidIn('id', ['usr_A3EyoEb2TO'])
    ->get();
```

This will return all users where the `id` is in the array of decoded Sqids.

#### Where Sqid not in

[](#where-sqid-not-in)

To get all models where the Sqid is not in a given array, you can use the `whereSqidNotIn` method:

```
$users = User::query()
    ->whereSqidNotIn('id', ['usr_A3EyoEb2TO'])
    ->get();
```

This will return all users where the `id` is not in the array of decoded Sqids.

### Validation Rule

[](#validation-rule)

There may be times where you need to validate a sqid in a form request. Laravel Sqids provides a `SqidsExists` rule to handle this automatically.

```
use RedExplosion\Sqids\Rules\SqidExists;

$validated = validator(
    ['customer_id' => 'cus_A3EyoEb2TO'],
    ['customer_id' => [new SqidExists(Customer::class)]],
)->validate();
```

The rule validates that the sqid can be decoded for the given model and that the model exists.

You can also add query constraints similar to Laravel's `Rule::exists`:

```
use RedExplosion\Sqids\Rules\SqidExists;

$rule = (new SqidExists(Post::class))
    ->where('team_id', $team->id)
    ->withoutTrashed();

$validated = validator(
    ['post' => 'pst_A3EyoEb2TO'],
    ['post' => [$rule]],
)->validate();
```

Available constraints:

- `where($column, $value)`
- `whereNot($column, $value)`
- `whereNull($column)`
- `whereNotNull($column)`
- `whereIn($column, $values)`
- `whereNotIn($column, $values)`
- `withoutTrashed()`
- `onlyTrashed()`

### Route model binding

[](#route-model-binding)

Laravel Sqids supports route model binding out of the box. Simply create a route as you normally would and we'll take care of the rest:

```
// GET /users/usr_A3EyoEb2TO
Route::get('users/{user}', function (User $user) {
    return "Hello $user->name";
});
```

### Finding a model from a Sqid

[](#finding-a-model-from-a-sqid)

One of the most powerful features of Laravel Sqids is being able to resolve a model instance from a given Sqid. This could be incredibly powerful when searching models across your application.

```
use RedExplosion\Sqids\Model;

$model = Model::find('usr_A3EyoEb2TO');
```

When we run the following, `$user` will be an instance of the `User` model for the given Sqid. If no model could be found, then `null` will be returned.

if you would like to throw an exception instead, you can use the `findOrFail` method which will throw an instance of the `ModelNotFoundException`:

```
use RedExplosion\Sqids\Model;

$model = Model::findOrFail('usr_A3EyoEb2TO');
```

Important

In order to use this feature, you must use prefixes for your Sqids.

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)

If you discover a security vulnerability, please send an e-mail to Ben Sherred via . All security vulnerabilities will be promptly addressed.

Credits
-------

[](#credits)

- [Ben Sherred](https://github.com/bensherred)
- [All Contributors](../../contributors)

License
-------

[](#license)

Laravel Sqids is open-sourced software licensed under the [MIT license](LICENSE.md).

###  Health Score

58

—

FairBetter than 98% of packages

Maintenance88

Actively maintained with recent releases

Popularity42

Moderate usage in the ecosystem

Community19

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 84.8% 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 ~70 days

Recently: every ~85 days

Total

13

Last Release

61d ago

Major Versions

1.x-dev → v2.0.02026-02-27

PHP version history (2 changes)v1.0.0PHP ^8.2

v2.0.0PHP ^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/4a81c5266912c039021715b77d96ad65b6aac3a76f39430331f31ea240f0c14e?d=identicon)[bensherred](/maintainers/bensherred)

---

Top Contributors

[![bensherred](https://avatars.githubusercontent.com/u/22666637?v=4)](https://github.com/bensherred "bensherred (78 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (4 commits)")[![mwikala](https://avatars.githubusercontent.com/u/39342367?v=4)](https://github.com/mwikala "mwikala (4 commits)")[![mrl22](https://avatars.githubusercontent.com/u/5681606?v=4)](https://github.com/mrl22 "mrl22 (2 commits)")[![ziming](https://avatars.githubusercontent.com/u/679513?v=4)](https://github.com/ziming "ziming (1 commits)")[![iDiegoNL](https://avatars.githubusercontent.com/u/10339564?v=4)](https://github.com/iDiegoNL "iDiegoNL (1 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (1 commits)")[![bencarr](https://avatars.githubusercontent.com/u/867691?v=4)](https://github.com/bencarr "bencarr (1 commits)")

---

Tags

hashidslaravelphpsqidslaravelred-explosionlaravel-sqids

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/red-explosion-laravel-sqids/health.svg)

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

###  Alternatives

[spatie/laravel-livewire-wizard

Build wizards using Livewire

4061.0M4](/packages/spatie-laravel-livewire-wizard)[tonysm/importmap-laravel

Use ESM with importmap to manage modern JavaScript in Laravel without transpiling or bundling.

148399.8k1](/packages/tonysm-importmap-laravel)[bensampo/laravel-embed

Painless responsive embeds for videos, slideshows and more.

142146.8k](/packages/bensampo-laravel-embed)[dragon-code/pretty-routes

Pretty Routes for Laravel

10058.7k4](/packages/dragon-code-pretty-routes)[laracraft-tech/laravel-useful-additions

A collection of useful Laravel additions!

58109.4k](/packages/laracraft-tech-laravel-useful-additions)[watson/nameable

Format names of users into full, familiar and abbreviated forms

299.7k](/packages/watson-nameable)

PHPackages © 2026

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