All Projects → Kyslik → laravel-filterable

Kyslik / laravel-filterable

Licence: MIT license
Laravel 5/6/7 package to handle filtering by query-string

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-filterable

bin
My bioinfo toolbox
Stars: ✭ 42 (-64.1%)
Mutual labels:  filtering
pushbullet
Pushbullet notifications channel for Laravel
Stars: ✭ 14 (-88.03%)
Mutual labels:  laravel-package
apposite
Conditionally apply Laravel validation rules, even when you don't have access to the validator instance.
Stars: ✭ 36 (-69.23%)
Mutual labels:  laravel-package
laravel route summary
Create a summary of all the laravel routes
Stars: ✭ 11 (-90.6%)
Mutual labels:  laravel-package
laravel-charts-css
Laravel component to create gorgeous Charts.css charts.
Stars: ✭ 105 (-10.26%)
Mutual labels:  laravel-package
spid-laravel
SPID authentication package for Laravel
Stars: ✭ 41 (-64.96%)
Mutual labels:  laravel-package
laravel-helper-functions
Laravel-specific and pure PHP Helper Functions.
Stars: ✭ 106 (-9.4%)
Mutual labels:  laravel-package
swaggervel
Swagger for Laravel
Stars: ✭ 70 (-40.17%)
Mutual labels:  laravel-package
laravel-dadata
PHP SDK Laravel пакет работы с сервисом DaData.ru, для исправления синтаксических ошибок в информации контактных данных клиентов сайта и вывода подсказок поля форм.
Stars: ✭ 39 (-66.67%)
Mutual labels:  laravel-package
airbrake-laravel
Laravel package for the Airbrake API, which supports Errbit
Stars: ✭ 16 (-86.32%)
Mutual labels:  laravel-package
laravel-snowflake
This Laravel package to generate 64 bit identifier like the snowflake within Twitter.
Stars: ✭ 94 (-19.66%)
Mutual labels:  laravel-package
lastfm
🎶 Last.fm API client for PHP. Comes with a Laravel service provider.
Stars: ✭ 17 (-85.47%)
Mutual labels:  laravel-package
laravel-embed
Effortless responsive embeds for videos, slideshows and more.
Stars: ✭ 106 (-9.4%)
Mutual labels:  laravel-package
voyager-page-blocks
A module to provide page blocks for Voyager 📝
Stars: ✭ 80 (-31.62%)
Mutual labels:  laravel-package
laravel-sybase
Connection and Laravel Eloquent driver for Sybase
Stars: ✭ 29 (-75.21%)
Mutual labels:  laravel-package
laravel-smart-facades
Strategy design pattern in laravel, the easiest way.
Stars: ✭ 84 (-28.21%)
Mutual labels:  laravel-package
bread-templates
BREAD Templates with Voyager
Stars: ✭ 19 (-83.76%)
Mutual labels:  laravel-package
world
A Laravel package which provides a list of the countries, states, cities, currencies, timezones and languages.
Stars: ✭ 479 (+309.4%)
Mutual labels:  laravel-package
gitup
Laravel package to upload git commits to server(s) via (s)ftp.
Stars: ✭ 20 (-82.91%)
Mutual labels:  laravel-package
laravel-test-watcher
Laravel Test Watcher
Stars: ✭ 20 (-82.91%)
Mutual labels:  laravel-package

Laravel Filterable

Latest Version on Packagist Build Status Total Downloads

This package allows you to easily handle database filtering through query strings. The idea is taken from one of the Jeffrey's videos (behind the paywall). One quick example might look like this: /users?filter-username=~joe will result in SQL query select * from users where "username" like '%joe%'.

Installation

You can install the package via composer:

composer require kyslik/laravel-filterable

If you are using Laravel 7:

composer require kyslik/laravel-filterable dev-L7

Laravel will discover the package by itself. If you feel old-school, disable auto-discovery and add Kyslik\LaravelFilterable\FilterableServiceProvider::class to the providers array in your config/app.php.

You may continue by publishing configuration by issuing following artisan command php artisan vendor:publish.

Introduction

Package lets you to create && apply two kinds of filters custom and generic.

Custom filters

Custom filters are just like in Jeffrey's video. You define a logic on a builder instance and package applies it via local scope.

Let's say a product requires displaying recently created records. You create a method recent($minutes = null) inside a filter class, which returns Builder instance:

public function recent($minutes = null): \Illuminate\Database\Eloquent\Builder
{
    $minutes = (is_numeric($minutes)) ? $minutes : 30;

    return $this->builder->where('created_at', '>=', Carbon\Carbon::now()->subMinutes($minutes));
}

Note: full example is shown later on

Generic filters

Generic filters are those defined in config file. By default, the package supports filtering timestamps, ranges, ins, booleans and strings.

/?filter-created_at=t>=1510952444
/?filter-id=><1,19
/?filter-id=i=1,5,10,12
/?filter-admin=b=yes
/?filter-username=joe
/?filter-username=~joe
/?filter-username=~joe&filter-admin=b=yes&filter-created_at=t=1510952444

Default operator matrix for generic filters

operator accepts description
= string equal
!= string not equal
> string greater than
< string less than
>= string equal or greater than
<= string equal or less than
~ string like
!~ string not like
>< comma separated list between
!>< comma separated list not between
i= comma separated list in
i!= comma separated list not in
b= 1, 0, true, false, yes, no equal
b!= 1, 0, true, false, yes, no not equal
t= UNIX timestamp equal
t!= UNIX timestamp not equal
t> UNIX timestamp greater than
t< UNIX timestamp less than
t>= UNIX timestamp equal or greater than
t<= UNIX timestamp equal or less than
t>< UNIX timestamp between
t!>< UNIX timestamp not between

Usage

While using both custom or generic filters you must:

  1. have local scope on model with the signature scopeFilter(Builder $query, FILTERNAME $filters)
  2. have particular (FILTERNAME) filter class that extends one of:
    • Kyslik\LaravelFilterable\Generic\Filter class - allows usage of both custom & generic filters
    • Kyslik\LaravelFilterable\Filter class - allows usage of only custom filters
  3. call a scope within a controller

make:filter command

You can use the following command to create a new filter.

php artisan make:filter SomeFilter

This will create a new Custom filter in the app/Filters directory. To create a Generic filter just add the --generic (-g) flag to the command:

php artisan make:filter SomeGenericFilter -g

Lastly, you can override the default namespace by changing the namespace config value e.g.

config/filterable.php

return [
    'namespace' => 'Http\Filters',
    ...
];

Example with custom filters

Let's say you want to use filterable on User model. You will have to create the filter class App/Filters/UserFilter.php (php artisan make:filter UserFilter), specify filterMap() and filter method (recent(...)) with the custom logic.

<?php
namespace App\Filters;

use Kyslik\LaravelFilterable\Filter;

class UserFilter extends Filter
{
    public function filterMap(): array
    {
        return ['recent' => ['recently', 'recent']];
    }

    public function recent($minutes = null)
    {
        $minutes = (is_numeric($minutes)) ? $minutes : 30;

        return $this->builder->where('created_at', '>=', \Carbon\Carbon::now()->subMinutes($minutes)->toDateTimeString());
    }
}

Note: filterMap() shall return an associative array where key is a method name and value is either alias or array of aliases

Now add a local scope to the User model via Filterable:

use Kyslik\LaravelFilterable\Filterable;

...
class User extends Model
{
    use Filterable;
    ...
}

Finally, call the scope in a controller like so:

use App\Filters\UserFilter;
...
public function index(User $user, UserFilter $filters)
{
    return $user->filter($filters)->paginate();
}

Now end-user can visit users?recent or users?recently or users?recent=25 and results will be filtered by recent() method defined in UserFilter class.

Example with generic filters

Let's say you want to use generic filters on User model. You will have to create filter class App/Filters/UserFilter.php (php artisan make:filter UserFilter -g) and specify $filterables just like below:

<?php
namespace App\Filters;

use Kyslik\LaravelFilterable\Generic\Filter;

class UserFilter extends Filter
{
    protected $filterables = ['id', 'username', 'email', 'created_at', 'updated_at'];
}

Next, you will have to add a local scope to the User model via Filterable:

use Kyslik\LaravelFilterable\Filterable;

...
class User extends Model
{
    use Filterable;
    ...
}

Finally, call the scope in a controller like so:

use App\Filters\UserFilter;
...
public function index(User $user, UserFilter $filters)
{
    return $user->filter($filters)->paginate();
}

Now you are ready to filter User model.

Note: behind the scenes ...\Generic\Filter class extends Filter class, therefore using ...\Generic\Filter also enables you to apply custom filters defined within the filter class

Additional configuration

While using generic filters you may define which generics should be allowed. Define settings() method in a filter class, see below:

use Kyslik\LaravelFilterable\Generic\Filter
...
class UserFilter extends Filter
{
    protected $filterables = ['id', 'username', 'email', 'created_at', 'updated_at'];

    protected function settings()
    {
        // global settings for this filter, pick either "except" or "only" logic
        $this->only(['=', '~', '!~']);
        // $this->except(['!=']);

        // settings applied only to some columns, these settings ignore the "global" settings above
        $this->for(['username', 'id'])->only(['!=', '>=', '=', '~']);
        $this->for(['id'])->only(['=', '!=', '~']); // settings for "id" will be re-written
    }
}

Additional features

Default filtering

In case you need to apply a filter when no filter is applied yet (determined by what query-string contains at the given request), you can use the following code in the controller:

public function index(User $user, UserFilter $filter)
{
    // will redirect and "apply" the `recent` and `filter-id` filters 
    // if not a single filter from UserFilter is applied
    $filter->default(['recent' => now()->toDateTimeString(), 'filter-id' => '!=5']);

    return $user->filter($filter)->paginate();
}

End-user is going be redirected from http://filters.test/users to http://filters.test/users?recent=2018-10-01 13:52:40&filter-id=!=5. In case the filter that you specify as default does not exist Kyslik\LaravelFilterable\Exceptions\InvalidArgumentException is thrown.

Caution: be careful of infinite redirects

You can read more about the feature in the original issue #10.

JoinSupport for filters

TBA

You can read more about the feature in the original PR #9.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].