Download the PHP package jedrzej/sortable without Composer
On this page you can find all versions of the php package jedrzej/sortable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package sortable
Sortable trait for Laravel's Eloquent models
This package adds sorting functionality to Eloquent models in Laravel 4/5/6.
You could also find those packages useful:
- Searchable - Allows filtering your models using request parameters
- Withable - Allows eager loading of relations using request parameters
- Pimpable - A meta package that combines Sortable, Searchable and Withable behaviours
Composer install
Add the following line to composer.json
file in your project:
"jedrzej/sortable": "0.0.12"
or run the following in the commandline in your project's root folder:
composer require "jedrzej/sortable" "0.0.12"
Setting up sortable models
In order to make an Eloquent model sortable, add the trait to the model and define a list of fields that the model can be sorted by.
You can either define a $sortable
property or implement a getSortableAttributes
method if you want to execute some logic to define
list of sortable fields.
In order to make all fields sortable put an asterisk *
in the list of sortable fields:
Sorting models
SortableTrait
adds a sorted()
scope to the model - you can pass it a query being an array of sorting conditions:
or it will use sort
parameter from the request as default:
Overwriting default sorting logic
It is possible to overwrite how sorting parameters are used and applied to the query by implementing a callback in your
model named sortFieldName
, e.g.:
Defining default sorting criteria
It is possible to define default sorting criteria that will be used if no sorting criteria are provided in the request or
passed to sorted
method of your model. Default sorting criteria should be defined in $defaultSortCriteria property, e.g.:
Defining default sorting order
By default asc is considered as default sorting order. It is possible to define default sorting order that will be used if no sorting order is provided in the request or
passed to sorted
method of your model. Default sorting order should be defined in $defaultSortOrder property, e.g.:
Additional configuration
If you are using sort
request parameter for other purpose, you can change the name of the parameter that will be
interpreted as sorting criteria by setting a $sortParameterName
property in your model, e.g.: