Download the PHP package neurony/laravel-sort without Composer
On this page you can find all versions of the php package neurony/laravel-sort. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-sort
Sort Eloquent model records by their attributes or relationships
- Overview
- Installation
- Usage
- Extra
Overview
This package allows you to sort Eloquent model records by their attributes, or via their relationships.
Relationship types that can be sorted by: hasOne
, belongsTo
Installation
Install the package via Composer:
Usage
Step 1
Your Eloquent models should use the Neurony\Sort\Traits\IsSortable
trait.
Step 2
You would access by whatever means the desired URI that specifies how the sort should behave:
Please note the sort
and direction
parameters!
These are very important if you're using the IsSortable
trait, as these two parameters are default for defining the:
- field to sort by (sort parameter)
- direction to sort in (direction parameter)
To see how to change these parameters, please see the Extra section.
Step 3
Once you've used the Neurony\Sort\Traits\IsSortable
trait in your Eloquent models and you've supplied the correct sorting parameters, you can sort the model records by using the sorted()
query scope present on the trait.
The sorted
query scope receives a mandatory first argument, that should be an associative array containing the field to sort by and the direction to sort in:
Extra
Sorting by relationship
To sort Eloquent model records by a relationship, use the following format when specifying the sort
parameter:
{relationship_name}.{relationship_attribute}
Please note that you can only sort by relationships of type belongsTo
or hasOne
.
Changing the sorting parameters
In Step 2 we've talked about the importance of specifying the parameters that tell the trait the field to sort by and the direction to sort it.
If you wish, those fields can be changed to other fields.
In order to do that, you'll have to create a Sort
object that will extend the abstract Sort
object that comes with this package.
After you've created the sort object, pass it as the second argument to the sorted
query scope when sorting your model records.
For the example above, your URI should look like this:
Credits
- Andrei Badea
- All Contributors
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
License
The MIT License (MIT). Please see LICENSE for more information.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
All versions of laravel-sort with dependencies
illuminate/contracts Version ^6.0
illuminate/support Version ^6.0
illuminate/database Version ^6.0