ZigaStrgar / orderable by ZStrgar

Ordering package for Laravel 5.x
628
20
2
Package Data
Maintainer Username: ZStrgar
Maintainer Contact: ziga_strgar@hotmail.com (ZigaStrgar)
Package Create Date: 2016-09-11
Package Last Update: 2016-10-08
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-24 15:01:01
Package Statistics
Total Downloads: 628
Monthly Downloads: 2
Daily Downloads: 0
Total Stars: 20
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Orderable Laravel Package

This is my very first Laravel package. I find it useful for me :) When I work with projects where I need to run a lot of ORDER BY queries.

Instalation

Add the Orderable package to your composer.json file.

{
    "require": {
        "zigastrgar/orderable": "^1.0"
    }
}

OR

Simply run this in command line

composer require zigastrgar/orderable

Usage

Go to any model and add this to the model.

use ZigaStrgar\Orderable\Orderable;

class Article extends Model {
    use Orderable;
    
    public function orderable(){
        return [
            'id' => 'DESC',
            'title' => 'ASC',
            'user_id'
        ];
    }
}

If you don't use the key like in user_id case it will default to DESC.

Running "Orderable"

It's super simple.

Article::all();

Apply only specific rule

From now on, you can also do something like this.

Article::order(); //Equals to Article::all();

or

Article::order(['title']);

and only rule for title will bi applied.

Running without "Orderable"

Same. Very simple stuff.

Article::unorderable();

No scopes applied.

Remove specific rule

Article::unorderable(['title']);

In this case the rule for title won't be applied.