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

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

johankladder/eloquent-sortable
==============================

Sortable behaviour for eloquent models

3.4.1(8y ago)01.1kMITPHP &gt;=7.0

Since Jul 15Compare

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

READMEChangelogDependencies (5)Versions (27)Used By (0)

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

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

[![Latest Version](https://camo.githubusercontent.com/dc972f67706fc0514415c2117eb20f5982a05cae49417da9ff1e808eed102d13/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f7370617469652f656c6f7175656e742d736f727461626c652e7376673f7374796c653d666c61742d737175617265)](https://github.com/spatie/eloquent-sortable/releases)[![SensioLabsInsight](https://camo.githubusercontent.com/5935e6b204b95175e3e2c187254cb6e83b84db66af7e44f7c594c8bb4f77d5f3/68747470733a2f2f696d672e736869656c64732e696f2f73656e73696f6c6162732f692f66623737363562392d373633322d343839372d383035342d3232643835623431666664612e737667)](https://insight.sensiolabs.com/projects/fb7765b9-7632-4897-8054-22d85b41ffda)[![Build Status](https://camo.githubusercontent.com/d344ce77f38a9f37191c0a3db2cece569740646e5bae5ec21a7a9007476b1725/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7370617469652f656c6f7175656e742d736f727461626c652e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/spatie/eloquent-sortable)[![Quality Score](https://camo.githubusercontent.com/84d6c27b3cf25804ab4a0111c60b294e200132b9e7fa75a92c86381772a654c0/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7370617469652f656c6f7175656e742d736f727461626c652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/spatie/eloquent-sortable)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![StyleCI](https://camo.githubusercontent.com/52713b0d5789b33f77180e8479710f60dcd0bac5bd50d61bc06b20d4e6c1773c/68747470733a2f2f7374796c6563692e696f2f7265706f732f32313836363233322f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/21866232)[![Total Downloads](https://camo.githubusercontent.com/b6ac38b09365445c585412000c7bbbeabe0f51baf15128267eee6ce03cb2670d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f656c6f7175656e742d736f727461626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/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).

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

[](#installation)

This package can be installed through Composer.

```
$ composer require spatie/eloquent-sortable

```

Usage
-----

[](#usage)

To add sortable behaviour to your model you must:

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

### Example

[](#example)

```
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

class MyModel extends Eloquent implements Sortable
{

    use SortableTrait;

    public $sortable = [
        'order_column_name' => 'order_column',
        'sort_when_creating' => true,
        'sort_by_group_column' => 'group_by_column'
    ];

    ...
}
```

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['sort_by_group_column']` the package will automatically order by table order. If you specify this value it will sort only the rows which are set as column in the same table;

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.

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

[](#postcardware)

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

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

We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).

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)

Support us
----------

[](#support-us)

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

Does your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/spatie). All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

License
-------

[](#license)

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

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 69.4% 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 ~45 days

Recently: every ~55 days

Total

26

Last Release

3271d 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/10651693?v=4)[Johan Kladder](/maintainers/johankladder)[@johankladder](https://github.com/johankladder)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (86 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (9 commits)")[![Mul-tiMedia](https://avatars.githubusercontent.com/u/12711809?v=4)](https://github.com/Mul-tiMedia "Mul-tiMedia (5 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)")[![johankladder](https://avatars.githubusercontent.com/u/10651693?v=4)](https://github.com/johankladder "johankladder (3 commits)")[![sebdesign](https://avatars.githubusercontent.com/u/667144?v=4)](https://github.com/sebdesign "sebdesign (1 commits)")[![srmklive](https://avatars.githubusercontent.com/u/839335?v=4)](https://github.com/srmklive "srmklive (1 commits)")[![Tjoosten](https://avatars.githubusercontent.com/u/5157609?v=4)](https://github.com/Tjoosten "Tjoosten (1 commits)")[![akoepcke](https://avatars.githubusercontent.com/u/5311185?v=4)](https://github.com/akoepcke "akoepcke (1 commits)")[![weiyongsheng](https://avatars.githubusercontent.com/u/3038860?v=4)](https://github.com/weiyongsheng "weiyongsheng (1 commits)")[![blueclock](https://avatars.githubusercontent.com/u/586174?v=4)](https://github.com/blueclock "blueclock (1 commits)")[![chuprik](https://avatars.githubusercontent.com/u/802946?v=4)](https://github.com/chuprik "chuprik (1 commits)")[![jorenvanhee](https://avatars.githubusercontent.com/u/231202?v=4)](https://github.com/jorenvanhee "jorenvanhee (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)")

---

Tags

laravelmodeleloquentsortablesortbehaviour

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

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

The Laravel magic applied to joins.

1.6k35.7M52](/packages/kirschbaum-development-eloquent-power-joins)[spatie/eloquent-sortable

Sortable behaviour for eloquent models

1.5k26.8M361](/packages/spatie-eloquent-sortable)[psalm/plugin-laravel

Psalm plugin for Laravel

3345.4M354](/packages/psalm-plugin-laravel)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8723.3M27](/packages/yajra-laravel-oci8)

PHPackages © 2026

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