konsulting/laravel-sorting
| Install | |
|---|---|
composer require konsulting/laravel-sorting |
|
| Latest Version: | 0.1.9 |
| PHP: | ^5.6 | ^7.0 | ^8.0 |
| License: | MIT |
| Last Updated: | Feb 5, 2026 |
| Links: | GitHub · Packagist |
Sorting
A set of utilities for sorting models in Laravel.
Installation
Install using composer: composer require konsulting/laravel-sorting
Usage
Sorting
- Add the
Konsulting\Laravel\Sorting\Sortabletrait to your model. - Set up the configuration for the model by adding the
$sortableSettingsproperty providing the allowed sortable fields and a default sort order.
protected static $sortableSettings = [
'sortable' => ['name', 'created_at', 'updated_at'],
'defaultSort' => '+name',
];
- In your view where you’d like to add a sortable link, include the following (example for a Post model):
{{ App\Post::sortableLink('name', 'Name') }}
- When you want retrive a sorted collection of models, use the
sort()method.
App\Post::sort()->paginate();
// The sort method will extract the sort variable from the request, unless you pass them through (e.g. if you store in the session).
Prioritise
-
Add the
Konsulting\Laravel\Sorting\Prioritisetrait to your model. -
Ensure your model has a column for 'priority'. By default, this is called 'priority' but can be overriden if needed by overriding the
determinePriorityColumnName()method in the model. -
Use the
prioritisescope to sort the retrieved results by priority.
Security
If you find any security issues, or have any concerns, please email keoghan@klever.co.uk, rather than using the issue tracker.
Contributing
Contributions are welcome and will be fully credited. We will accept contributions by Pull Request.
Please:
- Use the PSR-2 Coding Standard
- Add tests, if you’re not sure how, please ask.
- Document changes in behaviour, including readme.md.
Testing
We use PHPUnit.
Run tests using PHPUnit: vendor/bin/phpunit
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Cli error handling for console/command-line PHP applications.