Some additional features on top of https://github.com/spatie/eloquent-sortable
oddvalue/eloquent-sortable is a Laravel package for some additional features on top of https://github.com/spatie/eloquent-sortable.
It currently has 2 GitHub stars and 153.445 downloads on Packagist (latest version v2.2.0).
Install it with composer require oddvalue/eloquent-sortable.
Discover more Laravel packages by oddvalue
or browse all Laravel packages to compare alternatives.
Last updated
This package is an extension of Spatie's excellent Eloquent Sortable. It adds the following additional features:
$model->moveBefore(int|Sortable $target): void$model->moveAfter(int|Sortable $target): void$model->moveBetween(int|Sortable $before = null, Sortable $after = null): void$model->moveTo(int|Sortable $newPosition): voidNote: The move methods rebuild the entire sequence so this package is not recommended for very large datasets.
You can install the package via composer:
composer require oddvalue/eloquent-sortable
Implement the interface and use the trait.
class MyModel extends Model implements \Oddvalue\EloquentSortable\Sortable
{
use \Oddvalue\EloquentSortable\SortableTrait;
}
# New model automatically sorted to the end of the list upon creation
$model = MyModel::create(['title' => 'foo']);
# Move the model before the 5th item in the list
$model->moveBefore(MyModel::where('order_column', 5)->first());
// OR
$model->moveBefore(5);
# Move the model after the 5th item in the list
$model->moveAfter(MyModel::where('order_column', 5)->first());
// OR
$model->moveAfter(5);
# Move the model between the 5th and 6th item in the list
$model->moveBetween(
MyModel::where('order_column', 5)->first(),
MyModel::where('order_column', 6)->first()
);
// OR
$model->moveBetween(5, 6);
# This is useful when using a javacript library that provides the node before
# and after the location an item is dropped
# Move the model to the 5th item in the list
$model->moveTo(MyModel::where('order_column', 5)->first());
// OR
$model->moveTo(5);
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.