Download the PHP package thoss/laravel-gap-sort without Composer
On this page you can find all versions of the php package thoss/laravel-gap-sort. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-gap-sort
Sortable behaviour for Eloquent models with gap algorithm
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
Requirement
- PHP 8.1
- Laravel 9/10
- Your order column must be an integer (recommended is unsigned integer)
Installation
You can install this package using composer.
Just run the command below.
Optionally you can publish the config file with:
This is the content of the file that will be published in config/gap-sort.php
Usage
To add sortable behaviour to your model you must:
- Use the trait
Thoss\GapSort\Traits\Sortablein your Model. - Optionally specify which column will be used as the order column. The default is
order_column. - 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
- You can initialize an existing Table with the order gap, maybe in a Migration file
Use the sorting with an REST API
-
register
/sortRoute
(with the enabled resource registrar you can easily add the/sortRoute) - Dispatch the
SortModelJob in your Controller
Example Requests with a 100 gap
Item1 is sorted between 2 and 3
Item1 is sorted to the last
Item3 is sorted to the first
Testing
Alternatives
All versions of laravel-gap-sort with dependencies
illuminate/database Version ^9.0|^10.0
illuminate/support Version ^9.0|^10.0
spatie/laravel-package-tools Version ^1.9