PHP code example of itsmill3rtime / l5-filterable-sortable

1. Go to this page and download the library: Download itsmill3rtime/l5-filterable-sortable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

itsmill3rtime / l5-filterable-sortable example snippets

 php


namespace App\Filters;

use RafflesArgentina\FilterableSortable\QueryFilters;

class ArticleFilters extends QueryFilters
{
    public function title($query)
    {
        return $this->builder->where('title', 'LIKE', '%'.$query.'%');
    }

    public function author($query)
    {
        return $this->builder->where('user_id', $query);
    }

    public function category_id($query)
    {
        return $this->builder->where('category_id', $query);
    }
}
 php


namespace App\Sorters;

use RafflesArgentina\FilterableSortable\QuerySorters;

class ArticleSorters extends QuerySorters
{
    // These properties are optional:

    protected static $orderKey = 'orden'; // Fallback value is 'order'

    protected static $orderByKey = 'ordenarPor'; // Fallback value is 'orderBy'

    // And there are mandatory:

    protected static $defaultOrder = 'asc';

    protected static $defaultOrderBy = 'title';

    public function title()
    {
        return $this->builder->orderBy('title', $this->order());
    }

    public function published_at()
    {
        return $this->builder->orderBy('updated_at', $this->order())
                             ->orderBy('title', $this->order());
    }

}
 php
// ...

$items = Article::filter()->sorter()->paginate();

// ...