Download the PHP package 42phoenix42/eloquent-sortable without Composer
On this page you can find all versions of the php package 42phoenix42/eloquent-sortable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download 42phoenix42/eloquent-sortable
More information about 42phoenix42/eloquent-sortable
Files in 42phoenix42/eloquent-sortable
Package eloquent-sortable
Short Description Sortable behaviour for eloquent models
License MIT
Homepage https://github.com/42phoenix42/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 records of that model + 1.
The package also provides a query scope to fetch all the records in the right order.
Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
Installation
This package can be installed through Composer.
Usage
To add sortable behaviour to your model you must:
- Implement the
Spatie\EloquentSortable\Sortable
interface. - Use the trait
Spatie\EloquentSortable\SortableTrait
. - Optionally specify which column will be used as the order column. The default is
order_column
.
Example
If you don't set a value $sortable['order_column_name']
the package will assume that your order column name will be named order_column
.
If you don't set a value $sortable['sort_when_creating']
the package will automatically assign the highest order number to a new model;
Assuming that the db-table for MyModel
is empty:
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.
To sort using a column other than the primary key, use the setNewOrderByCustomColumn
-method.
As with setNewOrder
, setNewOrderByCustomColumn
will also accept an optional starting order 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:
Grouping
If your model/table has a grouping field (usually a foreign key): id,
user_id
, title, order_column
and you'd like the above methods to take it into considerations, you can create a buildSortQuery
method at your model:
This will restrict the calculations to fields value of the model instance.
Tests
The package contains some integration/smoke tests, set up with Orchestra. The tests can be run via phpunit.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Postcardware
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.
We publish all received postcards on our company website.
Credits
- Freek Van der Herten
- All Contributors
Alternatives
Support us
Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.
License
The MIT License (MIT). Please see License File for more information.