ravaelles/filterable

Package for handling record filtering in Laravel 5.*

v2.1.2 2017-06-29 16:08 UTC

This package is not auto-updated.

Last update: 2024-12-14 19:12:49 UTC


README

Software License

Package to handle filtering of records in Laravel 5.*, ready to work with MongoDB.

Define filters easily. Save time by using provided bootstrap view which displays all the filters and properly styles select elements (and far more!). Oh, and there's also a nice search box, too! :)

Install

Via Composer

$ composer require ravaelles/filterable

Usage

(1)   Open config/app.php and add to the end of providers this entry:

\Ravaelles\Filterable\FilterableServiceProvider::class,

(2)   Now open command line in root of your project and publish a view:

php artisan vendor:publish

Feel free to modify it, it's now in resources\views\packages\filterable\filtering.blade.php

(3)   Notice that filtering.blade.php uses @push('scripts') operator. It appends all the javascript scripts to the end of the html, when jQuery has already been loaded to avoid jQuery is not defined error. To make it work, please add @stack('scripts') part just after you load your last script using <script> tag. Notice the @stack blade operator is available since about Laravel 5.2.20.

(4)   Now in your model add this trait:

use Ravaelles\Filterable\Filterable as Filterable;
(..)
class MyModelName extends Model 
{
    use Filterable; // Add trait

Every model that you want to be filterable will need this trait.

(5)   It's time to define filters to be used (variable $filters). We will define some example filters so you can see how it works. In your controller, just where you retrieve the records, add this:

$filters = [

    // First filter
    'status' => [ // Db field name
        'Status' => [ // Field name to be shown for user
            1 => "Created", // [Values => Display names] for select element
            2 => "Awaiting signature",
            3 => "Canceled",
            4 => "Signed",
        ]
    ],
    
    // Second filter
    'template_id' => [
        'Template' => [0 => "No", 1 => "Yes"]
        // 'Template' => Template::orderBy('id', -1)->pluck('name', 'id')->all() // Or use something like this
    ],
    
    // You can add more filters here
    //'field_name' => [
    //     'Display name' => ["black" => "Black tea", "green" => "Green tea"]
    //],
];

$users = User::orderBy('id')
	->filterable($filters)
	->paginate(10);

Notice that you can apply your own, custom where clauses just before the ->filterable part.

(6)   Finally, in your view add this line which will automatically display the filters using selects:

@include('vendor.filterable.filtering')

Feel free to modify this file as you wish.

(7)   If you wish to use search box functionality then make sure to add ->searchable() line in your controller like this:

$users = User::orderBy('id')
    ->searchable()
	->filterable($filters)
	->paginate(10);

...and in your model add a list of fields you want to search against e.g.

public $searchable = [
    'name', 'email', 'address'
];

It will search for text from the search box in any of these fields.

(8)   You are now able to use this package and dynamically filter the records! :)

License

The MIT License (MIT). Please see License File for more information.