PHPackages                             acodingproject/laravel-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. acodingproject/laravel-filterable

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

acodingproject/laravel-filterable
=================================

Using URL query strings to filter Eloquent queries.

v1.0.6(2y ago)0802MITPHPPHP 8.\*

Since Nov 17Pushed 2y agoCompare

[ Source](https://github.com/acodingproject/laravel-filterable)[ Packagist](https://packagist.org/packages/acodingproject/laravel-filterable)[ Docs](https://github.com/kyslik/laravel-filterable)[ RSS](/packages/acodingproject-laravel-filterable/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (4)Dependencies (5)Versions (7)Used By (0)

- [Laravel Filterable](#laravel-filterable)
    - [Installation](#installation)
    - [Before we start](#before-we-start)
        - [Custom filters](#custom-filters)
        - [Generic filters](#generic-filters)
            - [Default operator matrix for generic filters](#default-operator-matrix-for-generic-filters)
    - [Usage](#usage)
        - [Example with custom filters](#example-with-custom-filters)
        - [Example with generic filters](#example-with-generic-filters)
            - [Additional configuration](#additional-configuration)
    - [Testing](#testing)
    - [Changelog](#changelog)
    - [Contributing](#contributing)
    - [Security](#security)
    - [Credits](#credits)
    - [License](#license)

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 kyslik/laravel-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`.

Before we start
---------------

[](#before-we-start)

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

### Custom filters

[](#custom-filters)

**Custom** filters are just like in Jeffrey's video. We define our logic on builder instance and package will apply it via [local scope](https://laravel.com/docs/5.5/eloquent#local-scopes).
Let's say we want to display recently created records. We create method `recent($minutes = null)` inside our filter class, this method returns Builder instance:

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

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

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
