|
| 1 | +# Laravel Eloquent Filters |
| 2 | + |
| 3 | +A package for Laravel that can be used to filter a model's records. |
| 4 | + |
| 5 | +## How does it work? |
| 6 | + |
| 7 | +Simply the package checks the request query for keys that match the corresponding filter keys set for the model then builds the query. |
| 8 | + |
| 9 | +Each model has a corresponding `filters` file where it's filters can be registered and mapped. |
| 10 | + |
| 11 | +Each registered `filter` is a unique file that contains the necessary functionality to build a query. |
| 12 | + |
| 13 | +This means that for models that have a `title` column, they can share the call the same filter file hence reducing code duplication. |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +Use composer to install the package: |
| 18 | + |
| 19 | +``` |
| 20 | +composer require miracuthbert/laravel-eloquent-filters |
| 21 | +``` |
| 22 | + |
| 23 | +## Setup |
| 24 | + |
| 25 | +The package takes advantage of Laravel Auto-Discovery, so it doesn't require you to manually add the ServiceProvider. |
| 26 | + |
| 27 | +If you don't use auto-discovery, add the ServiceProvider to the providers array in `config/app.php` |
| 28 | + |
| 29 | +```php |
| 30 | +Miracuthbert\Filters\EloquentFiltersServiceProvider::class |
| 31 | +``` |
| 32 | + |
| 33 | +If you want to publish the `config` file use the commands below in your console |
| 34 | + |
| 35 | +### Publish Config |
| 36 | + |
| 37 | +``` |
| 38 | +php artisan vendor:publish --provider=Miracuthbert\Filters\EloquentFiltersServiceProvider --tag=filters-config |
| 39 | +``` |
| 40 | + |
| 41 | +## Usage |
| 42 | + |
| 43 | +## Setting up a model |
| 44 | + |
| 45 | +A filter for a model generally extends the `Miracuthbert\Filters\FiltersAbstract`. |
| 46 | + |
| 47 | +It contains a list of filters that will should be applied to a the model and a map of `key/value` pair of filters list. |
| 48 | + |
| 49 | +To create one: |
| 50 | + |
| 51 | +First, create a model with `php artisan make:model` command. |
| 52 | + |
| 53 | +Then create a filter file for the model using: |
| 54 | + |
| 55 | +``` |
| 56 | +php artisan filter:model {name} |
| 57 | +
|
| 58 | +// example |
| 59 | +php artisan filter:make UserFilters |
| 60 | +
|
| 61 | +// within specific namespace |
| 62 | +php artisan filter:make Users\\UserFilters |
| 63 | +``` |
| 64 | + |
| 65 | +Switch `name` with the model name and preferably add suffix `Filters` to indicate it is a model filter, eg. `UserFilters` |
| 66 | + |
| 67 | +> Copy and add the block of code printed out in the console, to the related model. Do not forget to pull in the required namespaces. |
| 68 | +
|
| 69 | +### Using Filter in Controller |
| 70 | + |
| 71 | +After setup above in any controller that you call the model instance, call the `filter` scope passing an instance of the `Illuminate\Http\Request'. |
| 72 | + |
| 73 | +```php |
| 74 | +$users = User::filter($request)->get(); |
| 75 | +``` |
| 76 | + |
| 77 | +In case you have disabled appending of the filters query to a paginator, you could do it manually by using the `filters_query` helper: |
| 78 | + |
| 79 | +```php |
| 80 | +$users->appends(filters_query()); |
| 81 | +``` |
| 82 | + |
| 83 | +```blade |
| 84 | +// in blade view |
| 85 | +{{ $users->appends(filters_query())->links() }} |
| 86 | +``` |
| 87 | + |
| 88 | +## Creating a Filter |
| 89 | + |
| 90 | +All created filters by default will be placed within, `App\Filters` directory (namespace). |
| 91 | + |
| 92 | +To create a filter use: |
| 93 | + |
| 94 | +``` |
| 95 | +php artisan filter:make {name} |
| 96 | +
|
| 97 | +// example |
| 98 | +php artisan filter:make NameFilter |
| 99 | +
|
| 100 | +// within specific namespace |
| 101 | +php artisan filter:make Users\\NameFilter |
| 102 | +``` |
| 103 | + |
| 104 | +Switch `name` with the name of the filter, eg. `NameFilter` (normal filter), `CreatedOrder` (ordering filter) |
| 105 | + |
| 106 | +> When creating a filter, it is good to add `Filter` or `Order` to the name for ease of use. |
| 107 | +
|
| 108 | +You can then open the filter file and add your custom query functionality. See the `filter:make` command options for some preset templates. |
| 109 | + |
| 110 | +> Filters are basically blocks of code that extend `Illuminate\Database\Eloquent\Builder`, so you are not limited to the preset templates. |
| 111 | +
|
| 112 | +You can pass, the following options to the `filter:make` command, to make use of some of the common filter templates: |
| 113 | + |
| 114 | +- `column`, Indicates the column the filter should be implemented on |
| 115 | +- `bool`, Indicates if generated filter should be a boolean filter class |
| 116 | +- `null`, Indicates if generated filter should be a null check filter class |
| 117 | +- `order`, Indicates if generated filter should be an order filter class |
| 118 | +- `relation`, Generates a filter class for the given relationship |
| 119 | + |
| 120 | +## Registering Filters to Model Filters |
| 121 | + |
| 122 | +After creating a filter, to use it open a model filter and register a `key/value` pair under the `$filters` field. |
| 123 | + |
| 124 | +```php |
| 125 | + /** |
| 126 | + * A list of filters. |
| 127 | + * |
| 128 | + * @var array |
| 129 | + */ |
| 130 | + protected $filters = [ |
| 131 | + 'name' => NameFilter::class, |
| 132 | + 'email' => EmailFilter::class, |
| 133 | + 'created' => CreatedOrder::class, |
| 134 | + ]; |
| 135 | +``` |
| 136 | + |
| 137 | +## Setting Default Filters |
| 138 | + |
| 139 | +In a model filter, you can register a `key/value` pair under the `$defaultFilters` field, for filters you want to be applied by default. |
| 140 | + |
| 141 | +> `key` should be the same as the one registered on the `$filters` field, `value` should be an actual database value. |
| 142 | +
|
| 143 | +_Note_: Default filters should only be used for filters with `fixed` or `unchanging` values, eg. `true`, `false` |
| 144 | + |
| 145 | +```php |
| 146 | + /** |
| 147 | + * A list of default filters. |
| 148 | + * |
| 149 | + * @var array |
| 150 | + */ |
| 151 | + protected $defaultFilters = [ |
| 152 | + 'created' => 'desc', |
| 153 | + ]; |
| 154 | +``` |
| 155 | + |
| 156 | +> You can override default filters in the constructor of a model filter, by adding different checks. |
| 157 | +
|
| 158 | +```php |
| 159 | + // constructor |
| 160 | + |
| 161 | + if ($request->hasAny('cancelled', 'completed')) { |
| 162 | + $this->defaultFilters = [ |
| 163 | + 'upcoming' => 'false' |
| 164 | + ]; |
| 165 | + } |
| 166 | +``` |
| 167 | + |
| 168 | +## Console Commands |
| 169 | + |
| 170 | +There available commands within the package: |
| 171 | + |
| 172 | +- `filter:model`: Create a new filter class for a model |
| 173 | +- `filter:make`: Creates a new Eloquent filter class |
| 174 | + |
| 175 | +> Pass `--help` option to the commands to get more details on using them |
| 176 | +
|
| 177 | +## Security Vulnerabilities |
| 178 | + |
| 179 | +If you discover a security vulnerability, please send an e-mail to Cuthbert Mirambo via [miracuthbert@gmail.com](mailto:miracuthbert@gmail.com). All security vulnerabilities will be promptly addressed. |
| 180 | + |
| 181 | +## Credits |
| 182 | + |
| 183 | +- [Cuthbert Mirambo](https://github.com/miracuthbert) |
| 184 | + |
| 185 | +## License |
| 186 | + |
| 187 | +The project is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). |
0 commit comments