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(3mo ago)1255↓100%2MITPHPPHP ^8.2CI passing

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

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

42

—

FairBetter than 90% of packages

Maintenance80

Actively maintained with recent releases

Popularity17

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity49

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

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

[spatie/laravel-translatable

A trait to make an Eloquent model hold translations

2.4k22.1M350](/packages/spatie-laravel-translatable)[rtconner/laravel-likeable

Trait for Laravel Eloquent models to allow easy implementation of a 'like' or 'favorite' or 'remember' feature.

402385.8k5](/packages/rtconner-laravel-likeable)[moloquent/moloquent

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)

120114.2k7](/packages/moloquent-moloquent)[phaza/single-table-inheritance

Single Table Inheritance Trait

1515.8k](/packages/phaza-single-table-inheritance)

PHPackages © 2026

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