PHPackages                             metrixinfo/eloquent-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. metrixinfo/eloquent-sortable

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

metrixinfo/eloquent-sortable
============================

Sortable Trait for Laravel Eloquent models that supports grouping on multiple keys

v0.8.1(5y ago)47.2k2MITPHPPHP &gt;=7.1

Since Oct 14Pushed 5y ago1 watchersCompare

[ Source](https://github.com/metrixinfo/eloquent-sortable)[ Packagist](https://packagist.org/packages/metrixinfo/eloquent-sortable)[ Docs](https://github.com/metrixinfo/eloquent-sortable)[ RSS](/packages/metrixinfo-eloquent-sortable/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (2)Versions (15)Used By (0)

Sortable Behaviour for Eloquent Models
======================================

[](#sortable-behaviour-for-eloquent-models)

[![Build Status](https://camo.githubusercontent.com/e7028571c4cdb324145f5fe187e4734e954ba4893d7287265a93dfef128382b1/68747470733a2f2f7472617669732d63692e6f72672f6d6574726978696e666f2f656c6f7175656e742d736f727461626c652e7376673f6272616e63683d6d6173746572 "Build Status")](https://camo.githubusercontent.com/e7028571c4cdb324145f5fe187e4734e954ba4893d7287265a93dfef128382b1/68747470733a2f2f7472617669732d63692e6f72672f6d6574726978696e666f2f656c6f7175656e742d736f727461626c652e7376673f6272616e63683d6d6173746572)

This package provides a trait that adds sortable behaviour to an Eloquent model.

The value of the order column of a new record of a model is determined by the maximum value of the order column of all or a subset group of records of that model + 1.

The package also provides a query scope to fetch all the records in the right order.

This package is a fork of the popular [spatie/eloquent-sortable](https://github.com/spatie/eloquent-sortable) with added functionality to allow sorting on subsets of models as well as moving a model to a specific position.

Thank you to [Freek Van der Herten](https://murze.be) for sharing the original package.

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

[](#installation)

This package can be installed through Composer.

```
composer require metrixinfo/eloquent-sortable

```

Usage
-----

[](#usage)

To add sortable behaviour to your model you must:

1. Use the trait `Metrix\EloquentSortable\Sortable`.
2. Optionally specify which column will be used as the order column. The default is `display_order`.

### Examples

[](#examples)

*Simple ordered model*

```
use Metrix\EloquentSortable\Sortable;

class MyModel extends Eloquent
{

    use SortableTrait;

    public $sortable = [
        'sort_on_creating' => true,
        'order_column'      => 'display_order',
    ];

    ...
}
```

*Ordered model with a grouping column*

```
use Metrix\EloquentSortable\Sortable;

class MyModel extends Eloquent
{

    use SortableTrait;

    public $sortable = [
        'sort_on_creating'  => true,
        'order_column'      => 'display_order',
        'group_column'      => 'group_id',
    ];

    ...
}
```

*Ordered model grouped on multiple columns*

```
use Metrix\EloquentSortable\Sortable;

class MyModel extends Eloquent
{

    use SortableTrait;

    public $sortable = [
        'sort_on_creating'  => true,
        'order_column'      => 'display_order',
        'group_column'      => ['group_id','user_id'],
    ];

    ...
}
```

If you don't set a value for `$sortable['order_column']` the package will assume an order column name of `display_order`.

If you don't set a value `$sortable['sort_on_creating']` the package will automatically assign the next highest order value to the new model;

Assuming that the db table for `MyModel` is empty:

```
$myModel = new MyModel();
$myModel->save(); // order_column for this record will be set to 1

$myModel = new MyModel();
$myModel->save(); // order_column for this record will be set to 2

$myModel = new MyModel();
$myModel->save(); // order_column for this record will be set to 3
```

The trait also provides an ordered query scope. All models will be returned ordered by 'group' and then 'display\_order' if you have not applied a where() method for your group column on your query,

```
$orderedRecords = MyModel::ordered()->get();

$groupedOrderedRecords = MyModel::where('group_id', 2)->ordered()->get();

$allRecords = MyModel::ordered()->get();
```

You can set a new order for all the records using the `setNewOrder`-method

```
/**
 * the record for model id 3 will have record_column value 1
 * the record for model id 1 will have record_column value 2
 * the record for model id 2 will have record_column value 3
 */
MyModel::setNewOrder([3,1,2]);
```

Optionally you can pass the starting order number as the second argument.

```
/**
 * the record for model id 3 will have record_column value 11
 * the record for model id 1 will have record_column value 12
 * the record for model id 2 will have record_column value 13
 */
MyModel::setNewOrder([3,1,2], 10);
```

You can also move a model up or down with these methods:

```
$myModel->moveOrderDown();
$myModel->moveOrderUp();
```

You can also move a model to the first or last position:

```
$myModel->moveToStart();
$myModel->moveToEnd();
```

You can swap the order of two models:

```
MyModel::swapOrder($myModel, $anotherModel);
```

You can move a model to a specific position:

```
$myModel->moveToPosition(4);
```

Tests
-----

[](#tests)

The package contains some integration/smoke tests, set up with Orchestra. The tests can be run via the ./bin/test.sh script from the root directory.

```
$ ./bin/test.sh

```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 77.3% 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 ~69 days

Recently: every ~130 days

Total

14

Last Release

1873d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5a25316a87583541e845e7320f45bbea2d07faf7f4d77e3a3ce75f090b3a05f2?d=identicon)[metrixinfo](/maintainers/metrixinfo)

---

Top Contributors

[![hfmikep](https://avatars.githubusercontent.com/u/29101053?v=4)](https://github.com/hfmikep "hfmikep (17 commits)")[![metrixinfo](https://avatars.githubusercontent.com/u/43896192?v=4)](https://github.com/metrixinfo "metrixinfo (4 commits)")[![dorvidas](https://avatars.githubusercontent.com/u/3006552?v=4)](https://github.com/dorvidas "dorvidas (1 commits)")

---

Tags

laravelmodeleloquenttraitsortablesortorderdisplay order

### Embed Badge

![Health badge](/badges/metrixinfo-eloquent-sortable/health.svg)

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

###  Alternatives

[spatie/eloquent-sortable

Sortable behaviour for eloquent models

1.5k22.9M268](/packages/spatie-eloquent-sortable)[rutorika/sortable

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

299992.5k14](/packages/rutorika-sortable)[jedrzej/pimpable

Laravel 4/5/6 package that allows to dynamically filter, sort and eager load relations for your models using request parameters

105179.0k1](/packages/jedrzej-pimpable)[jedrzej/sortable

Sortable trait for Laravel's Eloquent models - sort your models using request parameters

54261.0k1](/packages/jedrzej-sortable)[nevadskiy/laravel-position

Arrange Laravel models in a given order.

2680.9k](/packages/nevadskiy-laravel-position)[highsolutions/eloquent-sequence

A Laravel package for easy creation and management sequence support for Eloquent models with elastic configuration.

121130.3k](/packages/highsolutions-eloquent-sequence)

PHPackages © 2026

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