PHPackages                             thoss/laravel-gap-sort - 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. thoss/laravel-gap-sort

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

thoss/laravel-gap-sort
======================

Sortable behaviour for Eloquent models with gap algorithm

v1.0.4(3y ago)07[2 issues](https://github.com/thoss/laravel-gap-sort/issues)MITPHPPHP ^8.1

Since Apr 25Pushed 3y ago1 watchersCompare

[ Source](https://github.com/thoss/laravel-gap-sort)[ Packagist](https://packagist.org/packages/thoss/laravel-gap-sort)[ RSS](/packages/thoss-laravel-gap-sort/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (8)Versions (6)Used By (0)

Sortable behaviour for Eloquent models with gap algorithm
=========================================================

[](#sortable-behaviour-for-eloquent-models-with-gap-algorithm)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![GitHub Workflow Status](https://camo.githubusercontent.com/0ee3c0e37229ed5ffa04a126a6bc5b79f5c251952f9cd3a6126de4d8934c401a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f74686f73732f6c61726176656c2d6761702d736f72742f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473)](https://camo.githubusercontent.com/0ee3c0e37229ed5ffa04a126a6bc5b79f5c251952f9cd3a6126de4d8934c401a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f74686f73732f6c61726176656c2d6761702d736f72742f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473)[![GitHub Workflow Status](https://camo.githubusercontent.com/22d292b74fd2f5b6e902652067858aaa117c6acd3b511d37fa9f0d680bef65c6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f74686f73732f6c61726176656c2d6761702d736f72742f7068707374616e2e796d6c3f6272616e63683d6d61696e266c6162656c3d5048505374616e)](https://camo.githubusercontent.com/22d292b74fd2f5b6e902652067858aaa117c6acd3b511d37fa9f0d680bef65c6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f74686f73732f6c61726176656c2d6761702d736f72742f7068707374616e2e796d6c3f6272616e63683d6d61696e266c6162656c3d5048505374616e)[![Latest Version](https://camo.githubusercontent.com/b35235910c01132a894d107bfe4f0a4cc3c1a0aeb52e73d40d7d409b079f41a0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f74686f73732f6c61726176656c2d6761702d736f72742e7376673f7374796c653d666c61742d737175617265)](https://github.com/thoss/laravel-gap-sort/releases)[![Latest Version on Packagist](https://camo.githubusercontent.com/9212236fd279696cef1369757b627689db547f465358ab53baf9601d5fa3119c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f74686f73732f6c61726176656c2d6761702d736f72742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/thoss/laravel-gap-sort)

This package provides a way to sort items in a table using the "Gap" algorithm, which is a more efficient way of reordering items in a table than using incremental values. It takes into account the gap between the order values of adjacent items and calculates the new order value for the main item based on the positions of the previous and next items.

So you can sort an item to any position with just one operation, you don't have to change other items!

[Description of the class Thoss\\GapSort\\SortModel](SORTMODEL_DESCRIPTION.md)

Requirement
-----------

[](#requirement)

- PHP 8.1
- Laravel 9/10
- Your order column must be an integer (recommended is unsigned integer)

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

[](#installation)

You can install this package using composer.
Just run the command below.

```
composer require thoss/laravel-gap-sort

```

Optionally you can publish the config file with:

```
php artisan vendor:publish --tag=gap-sort-config
```

This is the content of the file that will be published in `config/gap-sort.php`

```
return [
    /*
    * The name of the column that will be used to sort models.
    */
    'order_column' => 'order',

    /*
    * The gap between the sorted items
    */
    'order_gap' => 1000,

    /*
    * Indicates wheter the "/sort" route will be automaticaly added when you use the route ::register method
    */
    'resource_registrar_with_sort' => false,
];
```

Usage
-----

[](#usage)

To add sortable behaviour to your model you must:

1. Use the trait `Thoss\GapSort\Traits\Sortable` in your Model.
2. Optionally specify which column will be used as the order column. The default is `order_column`.
3. Optionally specify which gap between the sorted items you want to use. The default is `order_gap`.

> The larger the gap, the lower the probability that the table will have to be reinitialized

4. You can initialize an existing Table with the order gap, maybe in a Migration file

```
dispatch(new SortModel(modelString: YourModel::class, initTable:true));
```

### Use the sorting with an REST API

[](#use-the-sorting-with-an-rest-api)

1. register `/sort` Route
    (with the enabled resource registrar you can easily add the `/sort` Route)

```
Route::resource('salutations', 'SalutationsController', ['with' => ['sort']]);
```

2. Dispatch the `SortModel` Job in your Controller

```
use Thoss\GapSort\Requests\SortRequest;
use Thoss\GapSort\SortModel;

public function sort(SortRequest $request)
{
    return $this->dispatchSync(new SortModel(MyModel::class));
}
```

Example Requests with a 100 gap
-------------------------------

[](#example-requests-with-a-100-gap)

Item1 is sorted between 2 and 3

```
Current List:
- Item1 (order 100)
- Item2 (order 200)
- Item3 (order 300)

POST /api/myresource/sort
{
    "main": 1,
    "previous": 2,
    "next": 3,
}

After Sort:
- Item2 (order 200)
- Item1 (order 250)
- Item3 (order 300)

```

Item1 is sorted to the last

```
Current List:
- Item1 (order 100)
- Item2 (order 200)
- Item3 (order 300)

POST /api/myresource/sort
{
    "main": 1,
    "previous": 3,
}

After Sort:
- Item2 (order 200)
- Item3 (order 300)
- Item1 (order 350)

```

Item3 is sorted to the first

```
Current List:
- Item1 (order 100)
- Item2 (order 200)
- Item3 (order 300)

POST /api/myresource/sort
{
    "main": 3,
    "next": 1,
}

After Sort:
- Item3 (order 50)
- Item1 (order 100)
- Item2 (order 200)

```

Testing
-------

[](#testing)

```
composer test
```

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

[](#alternatives)

-
-

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity57

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 ~0 days

Total

5

Last Release

1112d ago

### Community

Maintainers

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

---

Top Contributors

[![thoss](https://avatars.githubusercontent.com/u/6714075?v=4)](https://github.com/thoss "thoss (3 commits)")

---

Tags

eloquentgaplaravelperformantphpsorttraitlaravelmodeleloquentsortablesortbehaviourgapperformant

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/thoss-laravel-gap-sort/health.svg)

```
[![Health](https://phpackages.com/badges/thoss-laravel-gap-sort/health.svg)](https://phpackages.com/packages/thoss-laravel-gap-sort)
```

###  Alternatives

[spatie/eloquent-sortable

Sortable behaviour for eloquent models

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

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[dyrynda/laravel-model-uuid

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

4802.8M8](/packages/dyrynda-laravel-model-uuid)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[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)
