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

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

app-ark/eloquent-sortable
=========================

Sortable behaviour for eloquent models

v0.0.2(6y ago)01891MITPHPPHP &gt;=5.6

Since Jul 11Pushed 6y ago1 watchersCompare

[ Source](https://github.com/app-ark/eloquent-sortable)[ Packagist](https://packagist.org/packages/app-ark/eloquent-sortable)[ Docs](https://github.com/spatie/eloquent-sortable)[ RSS](/packages/app-ark-eloquent-sortable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (3)Used By (1)

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).

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 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,
    ];

    ...
}
```

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;

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

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~35 days

Total

2

Last Release

2459d ago

### Community

Maintainers

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

---

Top Contributors

[![xiaohuilam](https://avatars.githubusercontent.com/u/6964962?v=4)](https://github.com/xiaohuilam "xiaohuilam (2 commits)")

---

Tags

laravelmodeleloquentsortablesortbehaviour

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[spatie/eloquent-sortable

Sortable behaviour for eloquent models

1.5k22.9M266](/packages/spatie-eloquent-sortable)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M70](/packages/mongodb-laravel-mongodb)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[jedrzej/sortable

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

54261.0k1](/packages/jedrzej-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)

PHPackages © 2026

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