PHPackages                             mhomayoun/laravel7-filterable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. mhomayoun/laravel7-filterable

ActiveLibrary[Search &amp; Filtering](/categories/search)

mhomayoun/laravel7-filterable
=============================

Using URL query strings to filter Eloquent queries.

3.0.2(6y ago)01561MITPHPPHP &gt;=7.3

Since Nov 17Pushed 5y agoCompare

[ Source](https://github.com/mhomayoun/laravel7-filterable)[ Packagist](https://packagist.org/packages/mhomayoun/laravel7-filterable)[ Docs](https://github.com/kyslik/laravel-filterable)[ RSS](/packages/mhomayoun-laravel7-filterable/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (7)Versions (16)Used By (0)

Laravel Filterable
==================

[](#laravel-filterable)

[![Latest Version on Packagist](https://camo.githubusercontent.com/2aa727c7e7ff912f00311d22febc5274e96b2a5a62c751cc98f4f3052b04706d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f4b79736c696b2f6c61726176656c2d66696c74657261626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/kyslik/laravel-filterable)[![Build Status](https://camo.githubusercontent.com/4c2ee28b14f2cb73c25ac99c0c2b1b94efc72a8fa78ddfb1f1fb7375fc4363b5/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f4b79736c696b2f6c61726176656c2d66696c74657261626c652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/Kyslik/laravel-filterable)[![Total Downloads](https://camo.githubusercontent.com/80fe18dd7f595042e30661bd6bb7a5e9f1607e49409393d8be3e565990a50ec1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6b79736c696b2f6c61726176656c2d66696c74657261626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/kyslik/laravel-filterable)

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)](https://laracasts.com/series/eloquent-techniques/episodes/4). One quick example might look like this: `/users?filter-username=~joe` will result in SQL query `select * from users where "username" like '%joe%'`.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require mhomayoun/laravel7-filterable
```

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](./config/filterable.php) by issuing following artisan command `php artisan vendor:publish`.

Introduction
------------

[](#introduction)

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

### Custom filters

[](#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](https://laravel.com/docs/5.7/eloquent#local-scopes).

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](https://github.com/Kyslik/laravel-filterable#example-with-generic-filters)

### Generic filters

[](#generic-filters)

**Generic** filters are those defined in [config file](./config/filterable.php). By default, the package supports filtering `timestamps`, `ranges`, `ins`, `booleans` and `strings`.

```
/?filter-created_at=t>=1510952444
/?filter-id=>``string`greater than`=``string`equal or greater than``UNIX timestampgreater than`t=`UNIX timestampequal or greater than`t 'Http\Filters',
    ...
];
```

### Example with custom 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.

```
