firevel / sortable
A simple trait to make your Laravel Eloquent models sortable with ease.
Installs: 1 662
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
README
A simple trait to make your Laravel Eloquent models sortable with ease.
Installation
Using Composer:
composer require firevel/sortable
Setup
-
Import the
Sortable
trait in your Eloquent model. -
Add a protected
$sortable
array property to your model. This array should list the fields you want to allow for sorting.
Example:
use Firevel\Sortable\Sortable; class User extends Model { use Sortable; /** * Fields allowed for sorting. * * @var array */ protected $sortable = ['id', 'name', 'email']; }
Usage
You can now easily sort your models using the sort()
query scope.
Ascending Order:
To sort by name
in ascending order:
User::sort(['name'])->get();
Descending Order:
To sort by id
in descending order:
User::sort(['-id'])->get();
The -
sign before the field name indicates descending order.