and48/table-filters

Making Eloquent models filterable.

1.0.1 2023-07-28 09:34 UTC

This package is auto-updated.

Last update: 2024-09-26 15:11:39 UTC


README

Package for making Eloquent models filterable.

Setup

Composer

Pull this package in through Composer

composer require and48/table-filters

Publish migrations

Publish the package migrations files to your application.

php artisan vendor:publish --provider="AND48\TableFilters\TableFiltersServiceProvider" --tag="migrations"

Run migrations.

php artisan migrate

Publish configuration

Publish the package configuration file to your application.

php artisan vendor:publish --provider="AND48\TableFilters\TableFiltersServiceProvider" --tag="config"

See configuration file (config/filters.php) yourself and make adjustments as you wish.

Usage

Create filters for model.

use AND48\TableFilters\Models\Filter;
...
User::addTableFilters([
    ['field' =>'id', 'type' => Filter::TYPE_NUMBER, 'caption' => 'ID'],
    ['field' =>'name', 'type' => Filter::TYPE_STRING, 'caption' => 'Name'],
    ['field' =>'birthday', 'type' => Filter::TYPE_DATE, 'caption' => 'Birthday'],
    ['field' =>'is_blocked', 'type' => Filter::TYPE_BOOLEAN, 'caption' => 'Is blocked'],
    ['field' =>'balance', 'type' => Filter::TYPE_NUMBER, 'caption' => 'Balance'],
    ['field' =>'status', 'type' => Filter::TYPE_ENUM, 'caption' => 'Status'],
    ['field' =>'parent_id', 'type' => Filter::TYPE_SOURCE, 'caption' => 'Parent user'],
]);
...
}

Use TableFilterable trait inside your Eloquent model(s).

Get your filters.

$filters = User::tableFilterList(true, [
                'status' => ['new', 'verified', 'active', 'suspended']]);

Get source data.

$filter = Filter::find($filter_id);
$source_data = $filter->sourceData($page, $search_query);

Load source data by route.

route('filters.source_data', ['filter_id' => 1, 'query' => '*', 'page' => 2]);

Filtering model.

$filters = [
    ['id' => 1, 'operator' => '!=', 'values' => [2, 3]],
    ['id' => 2, 'operator' => '~', 'values' => ['and', 'dy']],
    ['id' => 3, 'operator' => '>=', 'values' => ['1986-06-06']],
    ['id' => 4, 'operator' => '=', 'values' => [false]],
    ['id' => 6, 'operator' => '=', 'values' => ['new', 'verified']],
    ['id' => 7, 'operator' => '!=', 'values' => []],
];
$users = User::tableFilter($filters)->get();

For save, update or delete filters to storage (db table), please use resource route filters.storages.

Use it with client library table-filters-client