PHPackages                             mammothcoding/laravel-easy-filter - 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. mammothcoding/laravel-easy-filter

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

mammothcoding/laravel-easy-filter
=================================

Easy filter and sorter for indexlike requests

v1.0.0(2y ago)13MITPHPPHP &gt;=7.4

Since Jul 12Pushed 2y ago1 watchersCompare

[ Source](https://github.com/mammothcoding/laravel-easy-filter)[ Packagist](https://packagist.org/packages/mammothcoding/laravel-easy-filter)[ RSS](/packages/mammothcoding-laravel-easy-filter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

laravel-easy-filter
===================

[](#laravel-easy-filter)

Readme in other languages: [RU](https://github.com/mammothcoding/laravel-easy-filter/blob/master/README.ru.md)

[![alt text](./filters.jpg "laravel-easy-filter")](./filters.jpg)

[![Latest Stable Version](https://camo.githubusercontent.com/a16460c453cdc19ac4812fe04573c55bd2ebdd016bc71e068666e8cdd6dba806/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d616d6d6f7468636f64696e672f6c61726176656c2d656173792d66696c746572)](https://packagist.org/packages/mammothcoding/laravel-easy-filter)[![License](https://camo.githubusercontent.com/fabab79a6f45c55904b42892962188070f2bf1d2cd8106b13249d9e18a92297b/68747470733a2f2f706f7365722e707567782e6f72672f6d656864692d66617468692f656c6f7175656e742d66696c7465722f6c6963656e7365)](https://packagist.org/packages/mehdi-fathi/eloquent-filter)[![GitHub stars](https://camo.githubusercontent.com/78251e5278560af5182672f2abf776c69c019203aa1c0ae86f223dd66c4b9044/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6d616d6d6f7468636f64696e672f6c61726176656c2d656173792d66696c746572)](https://github.com/mammothcoding/laravel-easy-filter/stargazers)[![Monthly Downloads](https://camo.githubusercontent.com/8580905227152d756152a5e56adf77fc552ef8dd541a24e1450dcb56743a2c1f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f6d616d6d6f7468636f64696e672f6c61726176656c2d656173792d66696c7465723f636f6c6f723d79656c6c6f77)](https://packagist.org/packages/mammothcoding/laravel-easy-filter)[![Github downloads](https://camo.githubusercontent.com/27cd6ab6b9087f727e958e80f3ef7cf936c2b6c3aaaa3dc913eb74eb91b05280/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f646f776e6c6f6164732f6d616d6d6f7468636f64696e672f6c61726176656c2d656173792d66696c7465722f746f74616c2e737667)](https://github.com/mammothcoding/laravel-easy-filter.git)

Table of Content
----------------

[](#table-of-content)

- [Requirements](#electric_plug-Requirements)
- [Introduction](#microphone-Introduction)
- [Installation](#electric_plug-Installation)
- [Usage](#Usage)
    - [Simple Examples](#Simple-Examples)
    - [All filtering methods](#All-filtering-methods)
- [Methods](#Methods)
- [License](#License)

🔌 Requirements
--------------

[](#electric_plug-requirements)

- PHP 7.4+
- Laravel 6.0+

🎤 Introduction
--------------

[](#microphone-introduction)

Easy filter and sorter for indexlike requests and listings of models. It is possible to use several methods of filtering in a query and sorting the result.

🔌 Installation
--------------

[](#electric_plug-installation)

Run this Composer command to install the latest version

```
    $ composer require mammothcoding/laravel-easy-filter

```

Usage
-----

[](#usage)

For usage you need to form the correct request to the server and process it in our filtering service to get the result in a convenient form.

- #### Format of get request headers:

    [](#format-of-get-request-headers)

***for filtering:***

```
    http://{adress}/{path}?filter=[["{fieldname}","{filtering method}","{value}"]]

```

***for sorting:***

```
    http://{adress}/{path}?sort=[["{fieldname}","{asc/desc}"]]

```

### Simple Examples

[](#simple-examples)

- #### An example of processing an index request on the server to get a list of users of the User model in the route body in /routes/web.php :

    [](#an-example-of-processing-an-index-request-on-the-server-to-get-a-list-of-users-of-the-user-model-in-the-route-body-in-routeswebphp-)

```
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Mammothcoding\LaravelEasyFilter\EasyFilter;

Route::get('/users', static function (Request $request) {
    $filter = new EasyFilter('App\Models\User', $request); // Creating a filter object, specifying the model, passing the request data
    $filter->filter(); // Applying a filter
    $filter->sort(); // Sorting
    $result = $filter->getResultArray(); // Get the result in the array
    return view('users', ['res' => $result]); // Controller returns the rendered view with result
});
```

- #### An example of a get request for a model list with a specific value of one column:

    [](#an-example-of-a-get-request-for-a-model-list-with-a-specific-value-of-one-column)

```
http://0.0.0.0/users?filter=[["name","=","Armani Harber"]]

```

- #### An example of a get-request for a model list with a specific ending in the value of one column:

    [](#an-example-of-a-get-request-for-a-model-list-with-a-specific-ending-in-the-value-of-one-column)

```
http://0.0.0.0/users?filter=[["email","endswith","gmail.com"]]

```

Actions in code:

```
namespace App\Http\Controllers;

use App\User;
use Mammothcoding\LaravelEasyFilter\EasyFilter;

class UsersController
{
    public function index()
    {
        $filter = new EasyFilter('App\Models\User', request()); // Creating a filter object, specifying the model, passing the request data
        $result = $filter->filter()->toArray(); // Apply the filter and convert the resulting collection into an array

        return view('users', ['$result' => $result]); // Controller returns the rendered view with result
    }
}
```

- #### An example of a get request for a model list with several filtering, sorting and pagination methods:

    [](#an-example-of-a-get-request-for-a-model-list-with-several-filtering-sorting-and-pagination-methods)

```
http://0.0.0.0/users?filter=[["email","endswith","gmail.com"],["created_at","
