PHPackages                             eriksulymosi/eloquent-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. [Database &amp; ORM](/categories/database)
4. /
5. eriksulymosi/eloquent-sqids

ActiveLibrary[Database &amp; ORM](/categories/database)

eriksulymosi/eloquent-sqids
===========================

On-the-fly sqids for Laravel Eloquent models.

0.3.3(5mo ago)1255↓100%2MITPHPPHP ^8.2CI passing

Since Sep 27Pushed 5mo agoCompare

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

READMEChangelog (3)Dependencies (4)Versions (7)Used By (0)

> Using sqids instead of integer ids in urls and list items can be more appealing and clever. For more information visit [sqids.org](https://sqids.org/).

> This package is based on mtvs's [eloquent-hashids](https://github.com/mtvs/eloquent-hashids) package.

Eloquent-Sqids [![Build Status](https://github.com/eriksulymosi/eloquent-sqids/actions/workflows/build.yml/badge.svg)](https://github.com/eriksulymosi/eloquent-sqids/actions/workflows/build.yml/badge.svg)
============================================================================================================================================================================================================

[](#eloquent-sqids-)

This adds sqids to Laravel Eloquent models by encoding/decoding them on the fly rather than persisting them in the database. So no need for another database column and also higher performance by using primary keys in queries.

Features include:

- Generating sqids for models
- Resloving sqids to models
- Ability to customize sqid settings for each model
- Route binding with sqids (optional)

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

[](#installation)

Install the package with Composer:

```
$ composer require eriksulymosi/eloquent-sqids
```

Also, publish the vendor config files to your application (necessary for the dependencies):

```
$ php artisan vendor:publish
```

Setup
-----

[](#setup)

Base features are provided by using `HasSqid` trait then route binding with sqids can be added by using `SqidRouting`.

```
use Illuminate\Database\Eloquent\Model;
use ErikSulymosi\EloquentSqids\HasSqid;
use ErikSulymosi\EloquentSqids\SqidRouting;

Class Item extends Model
{
	use HasSqid, SqidRouting;
}
```

Usage
-----

[](#usage)

### Basics

[](#basics)

```
// Generating the model sqid based on its key
$item->sqid();

// Equivalent to the above but with the attribute style
$item->sqid;

// Finding a model based on the provided sqid or
// returning null on failure
Item::findBySqid($sqid);

// Finding a model based on the provided sqid or
// throwing a ModelNotFoundException on failure
Item::findBySqidOrFail($sqid);

// Decoding a sqid to its equivalent id
$item->sqidToId($sqid);

// Encoding an id to its equivalent sqid
$item->idToSqid($id);

// Getting the name of the sqid connection
$item->getSqidsConnection();
```

### Add the sqid to the serialized model

[](#add-the-sqid-to-the-serialized-model)

Set it as default:

```
use Illuminate\Database\Eloquent\Model;
use ErikSulymosi\EloquentSqids\HasSqid;

class Item extends Model
{
    use HasSqid;

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['sqid'];
}
```

or specify it specificly:

`return $item->append('sqid')->toJson();`

### Implicit Route Bindings

[](#implicit-route-bindings)

If you want to resolve implicit route bindings for the model using its hahsid value you can use `SqidRouting` in the model.

```
use Illuminate\Database\Eloquent\Model;
use ErikSulymosi\EloquentSqids\HasSqid;
use ErikSulymosi\EloquentSqids\SqidRouting;

class Item extends Model
{
    use HasSqid, SqidRouting;
}
```

It overwrites `getRouteKeyName()`, `getRouteKey()` and `resolveRouteBindingQuery()`to use the sqids as the route keys.

It supports the Laravel's feature for customizing the key for specific routes.

```
Route::get('/items/{item:slug}', function (Item $item) {
    return $item;
});
```

#### Customizing The Default Route Key Name

[](#customizing-the-default-route-key-name)

If you want to by default resolve the implicit route bindings using another field you can overwrite `getRouteKeyName()` to return the field's name to the resolving process and `getRouteKey()` to return its value in your links.

```
use Illuminate\Database\Eloquent\Model;
use ErikSulymosi\EloquentSqids\HasSqid;
use ErikSulymosi\EloquentSqids\SqidRouting;

class Item extends Model
{
    use HasSqid, SqidRouting;

    public function getRouteKeyName()
    {
        return 'slug';
    }

    public function getRouteKey()
    {
        return $this->slug;
    }
}
```

You'll still be able to specify the sqid for specific routes.

```
Route::get('/items/{item:sqid}', function (Item $item) {
    return $item;
});
```

#### Supporting The Other Laravel's Implicit Route Binding Features

[](#supporting-the-other-laravels-implicit-route-binding-features)

When using `SqidRouting` you'll still be able to use softdeletable and child route bindings.

```
Route::get('/items/{item}', function (Item $item) {
    return $item;
})->withTrashed();

Route::get('/user/{user}/items/{item}', function (User $user, Item $item) {
    return $item;
})->scopeBindings();
```

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance72

Regular maintenance activity

Popularity17

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 73.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 ~160 days

Recently: every ~198 days

Total

6

Last Release

155d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1b725606f4b49b43f0123e387d0c1ac7ddcd877c477b52a2eea7a29ab21ba6ae?d=identicon)[eriksulymosi](/maintainers/eriksulymosi)

---

Top Contributors

[![mtvs](https://avatars.githubusercontent.com/u/8286154?v=4)](https://github.com/mtvs "mtvs (59 commits)")[![eriksulymosi](https://avatars.githubusercontent.com/u/178145?v=4)](https://github.com/eriksulymosi "eriksulymosi (15 commits)")[![npostman](https://avatars.githubusercontent.com/u/5596683?v=4)](https://github.com/npostman "npostman (3 commits)")[![crynobone](https://avatars.githubusercontent.com/u/172966?v=4)](https://github.com/crynobone "crynobone (2 commits)")[![leewillis77](https://avatars.githubusercontent.com/u/1097338?v=4)](https://github.com/leewillis77 "leewillis77 (1 commits)")

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/eriksulymosi-eloquent-sqids/health.svg)

```
[![Health](https://phpackages.com/badges/eriksulymosi-eloquent-sqids/health.svg)](https://phpackages.com/packages/eriksulymosi-eloquent-sqids)
```

###  Alternatives

[owen-it/laravel-auditing

Audit changes of your Eloquent models in Laravel

3.4k33.0M95](/packages/owen-it-laravel-auditing)[staudenmeir/eloquent-json-relations

Laravel Eloquent relationships with JSON keys

1.1k5.8M24](/packages/staudenmeir-eloquent-json-relations)[bavix/laravel-wallet

It's easy to work with a virtual wallet.

1.3k1.1M11](/packages/bavix-laravel-wallet)[dragon-code/migrate-db

Easy data transfer from one database to another

15717.4k](/packages/dragon-code-migrate-db)[gearbox-solutions/eloquent-filemaker

A package for getting FileMaker records as Eloquent models in Laravel

6454.8k2](/packages/gearbox-solutions-eloquent-filemaker)[cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

9126.6k3](/packages/cybercog-laravel-ownership)

PHPackages © 2026

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