PHPackages                             sudo520/laravel-sortable - 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. sudo520/laravel-sortable

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

sudo520/laravel-sortable
========================

Simple package to add sorting functionality to your Laravel models. (fork of ninoman/laravel-sortable)

1.1.1(1y ago)02MITPHPPHP &gt;=8.0

Since Jan 9Pushed 1y agoCompare

[ Source](https://github.com/ahmed-imraan-ai/laravel-sortable)[ Packagist](https://packagist.org/packages/sudo520/laravel-sortable)[ RSS](/packages/sudo520-laravel-sortable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Make your Laravel models Sortable in a moment
=============================================

[](#make-your-laravel-models-sortable-in-a-moment)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

There are lots of situations when you need a sorting functionality for your models. Of course everyone wants simple package to cover all common use cases.

Know what? ***You've found it!***

This package will automatically apply sort index for your newly created models, and also handle all resorting stuff.

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

[](#installation)

You can install this package using composer. Just run a command bellow.

```
composer require sudo520/laravel-sortable

```

Usage
-----

[](#usage)

It's very easy to start with this package. Just use `Sudo520\LaravelSortable\Sortable` trait in your model, and add `sort_index` column in your models migration and into `$fillable` property.

```
use Sudo520\LaravelSortable\Sortable;

class MyModel extends Eloquent
{
    use Sortable;

    protected $fillable = [
        ...
        'sort_index',
        ...
    ];

    ...
}

class CreateMyModelsTable extends Migration
{
    public function up()
    {
        Schema::create('my_models', function (Blueprint $table) {
            ...
            $table->unsignedInteger('sort_index');
            ...
        });
    }

    public function down()
    {
        Schema::dropIfExists('my_models');
    }
}
```

But of course every cool package should be configurable. This one is too :)

Configurations
--------------

[](#configurations)

So if you want to change the sorting column (by default it is `sort_index`), you should set a `$sortIndexColumn` property in your model.

```
use Sudo520\LaravelSortable\Sortable;

class MyModel extends Eloquent
{
    use Sortable;

    public $sortIndexColumn = 'order';

    ...
}
```

How I've mentioned above your newly created model will be sorted automatically, but in case you don't want it, you always can set property `$setSortIndexOnCreating` to `false`

```
use Sudo520\LaravelSortable\Sortable;

class MyModel extends Eloquent
{
    use Sortable;

    public $setSortIndexOnCreating = false;

    ...
}
```

Let's imagine a situation, when you have **Users** and every **user** has many **Posts**. In this kind of situation if you would like to add sorting for **Posts**, it will be weird to sort all **Posts** together, of course you will want to sort them for each user (grouped by `user_id`).

It can be easily done by setting `$sortingParentColumn` property of your model name of the column by which you want to group your sorting. And your newly created **Posts** now will be sorted uniquely for their user.

```
use Sudo520\LaravelSortable\Sortable;

class Post extends Eloquent
{
    use Sortable;

    public $sortingParentColumn = 'user_id';

    ...
}
```

You also can configure start index of your models sorting, by default it will start from 1. To change it you should set `$startSortingFrom` property the number from which you want to start sorting.

```
use Sudo520\LaravelSortable\Sortable;

class MyModel extends Eloquent
{
    use Sortable;

    public $startSortingFrom = 0;

    ...
}

MyModel::create([...]); // sort_index 0
MyModel::create([...]); // sort_index 1
MyModel::create([...]); // sort_index 2
```

Helpful stuff
-------------

[](#helpful-stuff)

##### Simple but useful scopes

[](#simple-but-useful-scopes)

Trait also will add some functionality to your models. For example, if you want get your models sorted, just apply `sorted` scope on your models.

```
/*
    Models

    ['id' => 1, 'sort_index' => 2];
    ['id' => 2, 'sort_index' => 3];
    ['id' => 3, 'sort_index' => 1];
*/

MyModel::pluck('id'); // [1, 2, 3]
MyModel::sorted()->pluck('id'); // [3, 1, 2]
```

Also you can use `sortedDesc` scope, which how you have guessed will order models in descending order.

##### Methods

[](#methods)

Be sure this methods will make your life easier.

If you have two models and want to swap them use `swapSort` method:

```
MyModel::swapSort($modelOne, $modelTwo);
```

In order to manipulate your one model's sorting you can use those methods:

```
$myModel->moveSortIndexDown();
$myModel->moveSortIndexUp();
$myModel->toSortingTop();
$myModel->toSortingBottom();
```

And of course you can just update your model's property, which is responsible for sort index and all other entities will be reordered automatically.

```
use Sudo520\LaravelSortable\Sortable;

class MyModel extends Eloquent
{
    use Sortable;

    public $sortIndexColumn = 'order';

    ...
}

$one = $myModel::create([...]); //order 1
$two = $myModel::create([...]); //order 2
$three = $myModel::create([...]); //order 3
$four = $myModel::create([...]); //order 4

$four->update(['order' => 2]);
//$one -> order 1;
//$four -> order 2;
//$two -> order 3;
//$three -> order 4;
```

##### Conclusions

[](#conclusions)

It's a lightweight, easy to use package which you can easily integrate into your application. Feel free to report about issues and possible improvements.

Thank you!

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance41

Moderate activity, may be stable

Popularity2

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 57.1% 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

Unknown

Total

1

Last Release

492d ago

### Community

Maintainers

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

---

Top Contributors

[![ahmed-imraan-ai](https://avatars.githubusercontent.com/u/47724658?v=4)](https://github.com/ahmed-imraan-ai "ahmed-imraan-ai (4 commits)")[![ninoman](https://avatars.githubusercontent.com/u/25107388?v=4)](https://github.com/ninoman "ninoman (3 commits)")

---

Tags

laraveldatabasesortingordering

### Embed Badge

![Health badge](/badges/sudo520-laravel-sortable/health.svg)

```
[![Health](https://phpackages.com/badges/sudo520-laravel-sortable/health.svg)](https://phpackages.com/packages/sudo520-laravel-sortable)
```

###  Alternatives

[rutorika/sortable

Adds sortable behavior and ordering to Laravel Eloquent models. Grouping and many to many supported.

299992.5k14](/packages/rutorika-sortable)[waad/laravel-model-metadata

A robust Laravel package for handling metadata with JSON casting, custom relation names, and advanced querying capabilities.

823.1k](/packages/waad-laravel-model-metadata)[nevadskiy/laravel-geonames

Populate your database using the GeoNames service.

2715.1k](/packages/nevadskiy-laravel-geonames)

PHPackages © 2026

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