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

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

artistan/eloquent-sortable
==========================

Sortable behaviour for eloquent models

3.5.2.1(7y ago)068MITPHPPHP &gt;=7.0

Since Jul 15Pushed 7y ago1 watchersCompare

[ Source](https://github.com/Artistan/eloquent-sortable)[ Packagist](https://packagist.org/packages/artistan/eloquent-sortable)[ Docs](https://github.com/Artistan/eloquent-sortable)[ RSS](/packages/artistan-eloquent-sortable/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (3)Dependencies (5)Versions (32)Used By (0)

Sortable behaviour for Eloquent models
======================================

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

[![Latest Version](https://camo.githubusercontent.com/755c95f38a059ce102b78b853493b1ef123362c28623f52373a42bbed5326a0e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f417274697374616e2f656c6f7175656e742d736f727461626c652e7376673f7374796c653d666c61742d737175617265)](https://github.com/Artistan/eloquent-sortable/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/2d567b1aeacc7c130f78cdcd4a07fd39b6397cc8d1887a94a4a6213b9910c1ed/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f617274697374616e2f656c6f7175656e742d736f727461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/artistan/eloquent-sortable)

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 records of that model + 1.

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

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

Postcardware
------------

[](#postcardware)

You're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

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

[](#installation)

This package can be installed through Composer.

```
$ composer require Artistan/eloquent-sortable

```

Usage
-----

[](#usage)

To add sortable behaviour to your model you must:

1. specify that the model will conform to `Artistan\EloquentSortable\Sortable`
2. use the trait `Artistan\EloquentSortable\SortableTrait`
3. specify which column will be used as the order column

### Example

[](#example)

```
use Artistan\EloquentSortable\Sortable;
use Artistan\EloquentSortable\SortableTrait;

class MyModel extends Eloquent implements Sortable
{

    use SortableTrait;

    public $sortable = [
        'order_column_name' => 'order_column',
        'sort_when_creating' => true,
        /* 'order_unique' => ['another_key'], // optional additional keys for allowing multiple sorts in a table. */
    ];

    ...
}
```

If you don't set a value `$sortable['order_column_name']` the package will assume that your order column name will be named `order_column`.

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

If you don't set a value `$sortable['order_unique']` the package will assume that your model will sort all values. If it is set, then sorting will be done with matching the key(s) provided.

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 the ordered query scope
$orderedRecords = 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);
```

Tests
-----

[](#tests)

The package contains some integration/smoke tests, set up with Orchestra. The tests can be run via phpunit.

```
$ vendor/bin/phpunit

```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Freek Van der Herten](https://murze.be)
- [All Contributors](../../contributors)

Alternatives
------------

[](#alternatives)

- [Listify](https://github.com/lookitsatravis/listify)
- [Rutorike-sortable](https://github.com/boxfrommars/rutorika-sortable)

About Spatie
------------

[](#about-spatie)

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

License
-------

[](#license)

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

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 68% 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 ~50 days

Recently: every ~80 days

Total

30

Last Release

2906d ago

Major Versions

0.2.2 → 1.0.02015-02-13

1.1.2 → 2.0.02015-08-06

2.3.0 → 3.0.02016-10-21

PHP version history (3 changes)0.1.0PHP &gt;=5.4.0

2.2.0PHP &gt;=5.6.0

3.0.0PHP &gt;=7.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/801349?v=4)[Charles Peterson](/maintainers/Artistan)[@Artistan](https://github.com/Artistan)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (85 commits)")[![Artistan](https://avatars.githubusercontent.com/u/801349?v=4)](https://github.com/Artistan "Artistan (13 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (8 commits)")[![MatthiasDeWinter](https://avatars.githubusercontent.com/u/8791525?v=4)](https://github.com/MatthiasDeWinter "MatthiasDeWinter (4 commits)")[![pascalbaljet](https://avatars.githubusercontent.com/u/8403149?v=4)](https://github.com/pascalbaljet "pascalbaljet (3 commits)")[![driade](https://avatars.githubusercontent.com/u/5692232?v=4)](https://github.com/driade "driade (3 commits)")[![jorenvanhee](https://avatars.githubusercontent.com/u/231202?v=4)](https://github.com/jorenvanhee "jorenvanhee (1 commits)")[![brino](https://avatars.githubusercontent.com/u/986822?v=4)](https://github.com/brino "brino (1 commits)")[![mpociot](https://avatars.githubusercontent.com/u/804684?v=4)](https://github.com/mpociot "mpociot (1 commits)")[![nWidart](https://avatars.githubusercontent.com/u/882397?v=4)](https://github.com/nWidart "nWidart (1 commits)")[![Propaganistas](https://avatars.githubusercontent.com/u/6680176?v=4)](https://github.com/Propaganistas "Propaganistas (1 commits)")[![blueclock](https://avatars.githubusercontent.com/u/586174?v=4)](https://github.com/blueclock "blueclock (1 commits)")[![sebdesign](https://avatars.githubusercontent.com/u/667144?v=4)](https://github.com/sebdesign "sebdesign (1 commits)")[![weiyongsheng](https://avatars.githubusercontent.com/u/3038860?v=4)](https://github.com/weiyongsheng "weiyongsheng (1 commits)")[![chuprik](https://avatars.githubusercontent.com/u/802946?v=4)](https://github.com/chuprik "chuprik (1 commits)")

---

Tags

laravelmodeleloquentsortablesortbehaviour

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[spatie/eloquent-sortable

Sortable behaviour for eloquent models

1.5k24.7M307](/packages/spatie-eloquent-sortable)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.0M88](/packages/mongodb-laravel-mongodb)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k29.9M42](/packages/kirschbaum-development-eloquent-power-joins)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

592452.8k2](/packages/spiritix-lada-cache)[jedrzej/sortable

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

52272.9k1](/packages/jedrzej-sortable)[lemaur/eloquent-publishing

207.8k1](/packages/lemaur-eloquent-publishing)

PHPackages © 2026

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