bzarzuela / modelfilter by bzarzuela

Utility Class for Laravel 5 to assist in easily filtering paginated results.
1,576
5
3
Package Data
Maintainer Username: bzarzuela
Maintainer Contact: bryan@teleserv.ph (Bryan Zarzuela)
Package Create Date: 2015-08-28
Package Last Update: 2017-07-16
Language: PHP
License: MIT
Last Refreshed: 2024-11-24 03:03:01
Package Statistics
Total Downloads: 1,576
Monthly Downloads: 2
Daily Downloads: 0
Total Stars: 5
Total Watchers: 3
Total Forks: 0
Total Open Issues: 0

Model Filter

Utility Class for Laravel 5 to assist in easily filtering paginated results

Installation

composer require bzarzuela/modelfilter

Usage

Use in actions that show the list of models. In this example, it's the index action in the TicketsController.

public function index()
{
    $model_filter = new ModelFilter('tickets');

    $model_filter->setRules([
        'id' => ['primary'],
        'concern_types' => ['in', 'concern_type_id'],
        'created_from' => ['from', 'created_at'],
        'created_to' => ['to', 'created_at'],
    ]);

    $tickets = $model_filter->filter(Ticket::query())->paginate(30);

    $filters = $model_filter->getFormData();

    return view('tickets.index', compact('tickets', 'filters'));
}

The $filters variable passed to the view allows for the form to render the previously specified filters.