Download the PHP package metrixinfo/eloquent-sortable without Composer
On this page you can find all versions of the php package metrixinfo/eloquent-sortable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download metrixinfo/eloquent-sortable
More information about metrixinfo/eloquent-sortable
Files in metrixinfo/eloquent-sortable
Package eloquent-sortable
Short Description Sortable Trait for Laravel Eloquent models that supports grouping on multiple keys
License MIT
Homepage https://github.com/metrixinfo/eloquent-sortable
Informations about the package eloquent-sortable
Sortable Behaviour for Eloquent Models
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 or a subset group of records of that model + 1.
The package also provides a query scope to fetch all the records in the right order.
This package is a fork of the popular spatie/eloquent-sortable with added functionality to allow sorting on subsets of models as well as moving a model to a specific position.
Thank you to Freek Van der Herten for sharing the original package.
Installation
This package can be installed through Composer.
Usage
To add sortable behaviour to your model you must:
- Use the trait
Metrix\EloquentSortable\Sortable
. - Optionally specify which column will be used as the order column. The default is
display_order
.
Examples
Simple ordered model
Ordered model with a grouping column
Ordered model grouped on multiple columns
If you don't set a value for $sortable['order_column']
the package will assume an order column name of display_order
.
If you don't set a value $sortable['sort_on_creating']
the package will automatically assign the next highest order value to the new model;
Assuming that the db table for MyModel
is empty:
The trait also provides an ordered query scope. All models will be returned ordered by 'group' and then 'display_order' if you have not applied a where() method for your group column on your query,
You can set a new order for all the records using the setNewOrder
-method
Optionally you can pass the starting order number as the second argument.
You can also move a model up or down with these methods:
You can also move a model to the first or last position:
You can swap the order of two models:
You can move a model to a specific position:
Tests
The package contains some integration/smoke tests, set up with Orchestra. The tests can be run via the ./bin/test.sh script from the root directory.
License
The MIT License (MIT). Please see License File for more information.