├── README.md ├── composer.json └── src ├── EloquentFilterServiceProvider.php ├── Filterable.php └── QueryFilters.php /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Eloquent Filter 2 | 3 | [![Latest Stable Version](https://poser.pugx.org/nahidulhasan/eloquent-filter/v/stable)](https://packagist.org/packages/nahidulhasan/eloquent-filter) 4 | [![Total Downloads](https://poser.pugx.org/nahidulhasan/eloquent-filter/downloads)](https://packagist.org/packages/nahidulhasan/eloquent-filter) 5 | [![Latest Unstable Version](https://poser.pugx.org/nahidulhasan/eloquent-filter/v/unstable)](https://packagist.org/packages/nahidulhasan/eloquent-filter) 6 | [![License](https://poser.pugx.org/nahidulhasan/eloquent-filter/license)](https://packagist.org/packages/nahidulhasan/eloquent-filter) 7 | 8 | 9 | 10 | This simple package helps you filter Eloquent data using query filters. 11 | 12 | ## Installation 13 | 14 | Run the following command: 15 | 16 | ``` 17 | $ composer require nahidulhasan/eloquent-filter 18 | ``` 19 | 20 | 21 | ## Getting started 22 | 23 | Use the trait `NahidulHasan\EloquentFilter\Filterable` in your eloquent model. 24 | 25 | Create a new class by extending the class `NahidulHasan\EloquentFilter\QueryFilters` and define your custom filters as methods with one argument. Where function names are the filter argument name and the arguments are the value. 26 | 27 | Let's assume you want to allow to filter articles data. Please see the following code. 28 | 29 | ```php 30 | builder->where('title', 'like', '%' .$title.'%'); 71 | } 72 | } 73 | ``` 74 | 75 | With this class we can use the http query string : `title=article_name` or any combination of these filters. It is up to you to define if you will use AND wheres or OR. 76 | 77 | Now in the controller you can apply these filters like as described in below : 78 | 79 | 80 | ```php 81 | paginate(5); 104 | 105 | return view('articles.index', compact('articles'))->with('i', (request()->input('page', 1) - 1) * 5); 106 | } 107 | } 108 | ``` 109 | 110 | If you go to this link you will get all code: 111 | https://github.com/nahidulhasan/laravel-eloquent-query-filtering 112 | 113 | 114 | ### Thanks to : 115 | https://github.com/laracasts/Dedicated-Query-String-Filtering 116 | 117 | 118 | ### License 119 | 120 | Eloquent-Filter for Laravel is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT) 121 | 122 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nahidulhasan/eloquent-filter", 3 | "description": "Laravel eloquent query filter", 4 | "type": "package", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Nahidul Hasan", 9 | "email": "nahidul.cse@gmail.com" 10 | } 11 | ], 12 | "minimum-stability": "dev", 13 | "require": {}, 14 | 15 | "autoload": { 16 | "psr-4": { 17 | "NahidulHasan\\EloquentFilter\\": "src/" 18 | } 19 | }, 20 | 21 | "extra": { 22 | "laravel": { 23 | "providers": [ 24 | "NahidulHasan\\EloquentFilter\\EloquentFilterServiceProvider" 25 | ] 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/EloquentFilterServiceProvider.php: -------------------------------------------------------------------------------- 1 | apply($query); 18 | } 19 | } -------------------------------------------------------------------------------- /src/QueryFilters.php: -------------------------------------------------------------------------------- 1 | request = $request; 31 | } 32 | 33 | /** 34 | * Apply the filters to the builder. 35 | * 36 | * @param Builder $builder 37 | * @return Builder 38 | */ 39 | public function apply(Builder $builder) 40 | { 41 | $this->builder = $builder; 42 | 43 | foreach ($this->filters() as $name => $value) { 44 | if (! method_exists($this, $name)) { 45 | continue; 46 | } 47 | 48 | if (strlen($value)) { 49 | $this->$name($value); 50 | } else { 51 | $this->$name(); 52 | } 53 | } 54 | 55 | return $this->builder; 56 | } 57 | 58 | /** 59 | * Get all request filters data. 60 | * 61 | * @return array 62 | */ 63 | public function filters() 64 | { 65 | return $this->request->all(); 66 | } 67 | } --------------------------------------------------------------------------------