web-id/flan

FLAN - Filter Like A Ninja

Fund package maintenance!
web-id-fr

Installs: 2 519

Dependents: 0

Suggesters: 0

Security: 0

Stars: 5

Watchers: 9

Forks: 1

Open Issues: 0

Type:project

0.1.0 2021-06-28 08:16 UTC

README

Installation

Require this package with composer.

composer require web-id/flan

FLAN uses Laravel Package Auto-Discovery, and doesn't require you to manually add the ServiceProvider.

Copy the package config to your local config with the publish command:

php artisan vendor:publish --provider="WebId\Flan\FlanServiceProvider"

Finally, run the filter tables migration

php artisan migrate

Usage

You can create a filter with:

php artisan filter:create User

or eventually just the Filter class:

php artisan make:filter:class User

or just the Filter config:

php artisan make:filter:config User

Filter configuration

You can find the configuration files for your Filters in the folder config/FilterConfigs

A configuration file is made of two entries name and filters:

return [
    'name' => 'myfilter',
    'filters' => [
        [
            'text' => 'Model ID',
            'name' => 'id',
            'active' => true,
            'field' => [
                'type' => 'number',
            ],
        ],
        // [ ... ]
    ],
];

Configuration for any field type:

  • filters.*.text is the HTML input label
  • filters.*.name is the HTML input name attribute
  • filters.*.active determines if the data will be shown in the table
  • filters.*.filterable determines if the filter input will be shown for this column
  • filters.*.field contains options to apply on the input
  • filters.*.field.type is the input type, it can be one of those: checkbox, date, number, select, text

Configuration specific to select type:

  • filters.*.field.options contains the list of the available select options. Here an example:
'options' => [
    [
        'value' => '0',
        'text' => 'Disabled',
    ],
    [
        'value' => '1',
        'text' => 'Enabled',
    ],
    // [ ... ],
],

Filter class

The custom_select definition attribute

Let's say you are defining a BookFilter class, and you want to format the number of pages value:

$this->setDefinition('number_of_pages', [
    'custom_select' => 'CONCAT(`number_of_pages`, " pages")',
]);

The join definition attribute

Let's say you are defining a BookFilter class, and you want to be able to filter on the book's author birth city for example.

If you want to use a custom select with a join clause, in your Filter class constructor you can do this:

$this->setDefinition('author_birth_city', [
    'join' => 'leftJoinAuthorsTable',
    'custom_select' => '`authors`.`birth_city`',
]);

Then, you need to add a method named after your join parameter to apply the join on the query, in this example:

protected function leftJoinAuthorsTable(): void
{
    $this->query->leftJoin(
        'authors',
        'books.author_id',
        '=',
        'authors.id'
    );
}

Credits

License

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