Skip to content

A simple trait to make your Laravel Eloquent models sortable with ease.

License

Notifications You must be signed in to change notification settings

firevel/sortable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Sortable

A simple trait to make your Laravel Eloquent models sortable with ease.

Installation

Using Composer:

composer require firevel/sortable

Setup

  1. Import the Sortable trait in your Eloquent model.

  2. 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.

About

A simple trait to make your Laravel Eloquent models sortable with ease.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages