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

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

silverd/laravel-sortable
========================

Sortable behaviour for eloquent models

0136↓75%PHP

Since Apr 20Pushed 1y agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

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

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

@thanks spatie/eloquent-sortable

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

[](#installation)

This package can be installed through Composer.

```
composer require silverd/laravel-sortable:dev-master

```

Optionally you can publish the config file with:

```
php artisan vendor:publish --provider="Silverd\LaravelSortable\ServiceProvider"
```

Usage
-----

[](#usage)

To add sortable behaviour to your model you must:

1. Implement the `Silverd\LaravelSortable\Sortable` interface.
2. Use the trait `Silverd\LaravelSortable\SortableTrait`.
3. Optionally specify which column will be used as the order column. The default is `weight`.

### Example

[](#example)

```
use Silverd\LaravelSortable\Sortable;
use Silverd\LaravelSortable\SortableTrait;

class MyModel extends Eloquent implements Sortable
{
    use SortableTrait;

    public $sortable = [
        'sort_column_name' => 'weight',
        'sort_when_creating' => 'end',
    ];
    ...
}
```

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

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

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

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

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

$myModel = new MyModel();
$myModel->save(); // weight 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 weight value 1
 * the record for model id 1 will have weight value 2
 * the record for model id 2 will have weight 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 weight value 11
 * the record for model id 1 will have weight value 12
 * the record for model id 2 will have weight value 13
 */
MyModel::setNewOrder([3,1,2], 10);
```

To sort using a column other than the primary key, use the `setNewOrderByCustomColumn`-method.

```
/**
 * the record for model uuid '7a051131-d387-4276-bfda-e7c376099715' will have weight value 1
 * the record for model uuid '40324562-c7ca-4c69-8018-aff81bff8c95' will have weight value 2
 * the record for model uuid '5dc4d0f4-0c88-43a4-b293-7c7902a3cfd1' will have weight value 3
 */
MyModel::setNewOrderByCustomColumn('uuid', [
   '7a051131-d387-4276-bfda-e7c376099715',
   '40324562-c7ca-4c69-8018-aff81bff8c95',
   '5dc4d0f4-0c88-43a4-b293-7c7902a3cfd1'
]);
```

As with `setNewOrder`, `setNewOrderByCustomColumn` will also accept an optional starting order argument.

```
/**
 * the record for model uuid '7a051131-d387-4276-bfda-e7c376099715' will have weight value 10
 * the record for model uuid '40324562-c7ca-4c69-8018-aff81bff8c95' will have weight value 11
 * the record for model uuid '5dc4d0f4-0c88-43a4-b293-7c7902a3cfd1' will have weight value 12
 */
MyModel::setNewOrderByCustomColumn('uuid', [
   '7a051131-d387-4276-bfda-e7c376099715',
   '40324562-c7ca-4c69-8018-aff81bff8c95',
   '5dc4d0f4-0c88-43a4-b293-7c7902a3cfd1'
], 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);
```

License
-------

[](#license)

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

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity15

Early-stage or recently created project

 Bus Factor1

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

### Community

Maintainers

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

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (109 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (9 commits)")[![silverd](https://avatars.githubusercontent.com/u/1514608?v=4)](https://github.com/silverd "silverd (8 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (5 commits)")[![MatthiasDeWinter](https://avatars.githubusercontent.com/u/8791525?v=4)](https://github.com/MatthiasDeWinter "MatthiasDeWinter (4 commits)")[![driade](https://avatars.githubusercontent.com/u/5692232?v=4)](https://github.com/driade "driade (3 commits)")[![pascalbaljet](https://avatars.githubusercontent.com/u/8403149?v=4)](https://github.com/pascalbaljet "pascalbaljet (3 commits)")[![riasvdv](https://avatars.githubusercontent.com/u/3626559?v=4)](https://github.com/riasvdv "riasvdv (2 commits)")[![michelmegens](https://avatars.githubusercontent.com/u/718043?v=4)](https://github.com/michelmegens "michelmegens (1 commits)")[![mpociot](https://avatars.githubusercontent.com/u/804684?v=4)](https://github.com/mpociot "mpociot (1 commits)")[![newtongamajr](https://avatars.githubusercontent.com/u/8673343?v=4)](https://github.com/newtongamajr "newtongamajr (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)")[![RalphMRivera](https://avatars.githubusercontent.com/u/1270019?v=4)](https://github.com/RalphMRivera "RalphMRivera (1 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)")[![swapnilsarwe](https://avatars.githubusercontent.com/u/166912?v=4)](https://github.com/swapnilsarwe "swapnilsarwe (1 commits)")[![Tjoosten](https://avatars.githubusercontent.com/u/5157609?v=4)](https://github.com/Tjoosten "Tjoosten (1 commits)")[![twickstrom](https://avatars.githubusercontent.com/u/775346?v=4)](https://github.com/twickstrom "twickstrom (1 commits)")[![weiyongsheng](https://avatars.githubusercontent.com/u/3038860?v=4)](https://github.com/weiyongsheng "weiyongsheng (1 commits)")

### Embed Badge

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

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

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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