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(6mo ago)13212MITPHPPHP ^8.2CI passing

Since Sep 27Pushed 6mo 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 3w 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

39

—

LowBetter than 85% of packages

Maintenance66

Regular maintenance activity

Popularity19

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

200d 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

[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k29.9M42](/packages/kirschbaum-development-eloquent-power-joins)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.0M88](/packages/mongodb-laravel-mongodb)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8723.1M23](/packages/yajra-laravel-oci8)[bavix/laravel-wallet

It's easy to work with a virtual wallet.

1.3k1.2M18](/packages/bavix-laravel-wallet)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.4M2](/packages/glushkovds-phpclickhouse-laravel)[fleetbase/core-api

Core Framework and Resources for Fleetbase API

1232.2k16](/packages/fleetbase-core-api)

PHPackages © 2026

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