|
| 1 | +# labrodev/laravel-filter-components |
| 2 | + |
| 3 | +--- |
| 4 | +This repo is Laravel package to extend filtering functionality in Laravel projects. If you have a list with items and you use spatie/laravel-query-builder to filter them, this package could be useful for you. |
| 5 | + |
| 6 | +## QueryBuilder classes |
| 7 | + |
| 8 | +In this package you may find some custom part of Query Builder to extend filtering logic. |
| 9 | + |
| 10 | +**DateRangeFilter** |
| 11 | + |
| 12 | +QueryBuilder class which implements a logic to filter by range of dates using WhereBetween. |
| 13 | + |
| 14 | +**IsNotNullFilter** |
| 15 | + |
| 16 | +QueryBuilder class which implements a logic to filter by whereNull or whereNotNull depend on the given input value. Could be used when we just need to filter by some flag behind which there is a logic (like: Have unpaid invoices - Yes/No). |
| 17 | + |
| 18 | +**WhereInFilter** |
| 19 | + |
| 20 | +QueryBuilder class which implements a logic to filter b whereIn using given array of values (good for multiple selects as filters). |
| 21 | + |
| 22 | +## View components |
| 23 | + |
| 24 | +Also in this package you may find filter components that render filter component depends on type and logic. |
| 25 | + |
| 26 | +You may public vendor views from this package to implement your own styles for filter components blade templates and to adjust it to your layout and theme. |
| 27 | + |
| 28 | +By default filter components in blade use Bootstrap classes. |
| 29 | + |
| 30 | +**Boolean filter** |
| 31 | + |
| 32 | +View component to render a filter with select options No,Yes (or custom options defined in component attribute). |
| 33 | + |
| 34 | +**Custom select filter** |
| 35 | + |
| 36 | +View component to render a filter with custom select options as filter options. |
| 37 | + |
| 38 | +**Date range filter** |
| 39 | + |
| 40 | +View component to render a filter with two date inputs as date range (start date and end date). |
| 41 | + |
| 42 | +**Input filter** |
| 43 | + |
| 44 | +View component to render a filter with text input. |
| 45 | + |
| 46 | +**Multiple select field** |
| 47 | + |
| 48 | +View component to render a filter with multiple select options from given Eloquent Model. |
| 49 | + |
| 50 | +**Select field** |
| 51 | + |
| 52 | +View component to render a filter with select options from given Eloquent Model. |
| 53 | + |
| 54 | +**Link** |
| 55 | + |
| 56 | +View component to render a sort field. |
| 57 | + |
| 58 | +## Installation |
| 59 | + |
| 60 | +You can install the package via composer: |
| 61 | + |
| 62 | +```bash |
| 63 | +composer require labrodev/laravel-filter-components |
| 64 | +``` |
| 65 | + |
| 66 | +Optionally, you can publish the views to implement them to your layout. |
| 67 | + |
| 68 | +```bash |
| 69 | +php artisan vendor:publish --tag=filter-components-views |
| 70 | +``` |
| 71 | + |
| 72 | +Optionally, you can publish the view components to extend the logic you need. |
| 73 | + |
| 74 | +```bash |
| 75 | +php artisan vendor:publish --tag=filter-components-components |
| 76 | +``` |
| 77 | + |
| 78 | +## Usage |
| 79 | + |
| 80 | +### QueryBuilder classes |
| 81 | + |
| 82 | +Let's assume that you are familiar with [Spatie\QueryBuilder](https://spatie.be/docs/laravel-query-builder/v5/introduction) and already implemented filtering logic using Spatie\QueryBuilder. |
| 83 | + |
| 84 | +You may extend usage by using QueryBuilder classes from this package: DateRangeFilter, WhereInFilter, IsNotNullFilter. |
| 85 | + |
| 86 | +```php |
| 87 | +use Illuminate\Http\Request; |
| 88 | +use Labrodev\Filters\QueryBuilder\DateRangeFilter; |
| 89 | +use Labrodev\Filters\QueryBuilder\WhereInFilter; |
| 90 | +use Labrodev\Filters\QueryBuilder\IsNotNullFilter; |
| 91 | +use Spatie\QueryBuilder\AllowedFilter; |
| 92 | +use Spatie\QueryBuilder\QueryBuilder; |
| 93 | + |
| 94 | +class YourQuery extends QueryBuilder |
| 95 | +{ |
| 96 | + public function __construct(Request $request) |
| 97 | + { |
| 98 | + $query = YourModel::query(); |
| 99 | + |
| 100 | + parent::__construct($query, $request); |
| 101 | + |
| 102 | + //DateRangeFilter |
| 103 | + $this->allowedFilters([ |
| 104 | + AllowedFilter::custom('filter_name', new DateRangeFilter(), 'table_column') |
| 105 | + ]); |
| 106 | + |
| 107 | + //DateRangeFilter |
| 108 | + $this->allowedFilters([ |
| 109 | + AllowedFilter::custom('filter_name', new WhereInFilter(), 'table_column') |
| 110 | + ]); |
| 111 | + |
| 112 | + //IsNotNullFilter |
| 113 | + $this->allowedFilters([ |
| 114 | + AllowedFilter::custom('filter_name', new IsNotNullFilter(), 'table_column') |
| 115 | + ]); |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +### View components |
| 121 | + |
| 122 | +Let's consider that you want to have a filtering in your CRUD list. |
| 123 | + |
| 124 | +There could be a filter block. Let's assume to may have a form for your filters. |
| 125 | + |
| 126 | +```blade |
| 127 | +<form action="your filter routing" method="GET"> |
| 128 | +<!-- and here could be components from this package --> |
| 129 | +<button type="submit"></button> |
| 130 | +</form> |
| 131 | +``` |
| 132 | + |
| 133 | +**Boolean filter** |
| 134 | + |
| 135 | +```blade |
| 136 | +<x-filter-boolean-field |
| 137 | + field="filter[{{ $filterField }}]" |
| 138 | + name="{{ __('Filter label') }}" |
| 139 | + options="{{__('Your option 1') }},{{ __('Your option 2')}}"> |
| 140 | +</x-filter-boolean-field> |
| 141 | +``` |
| 142 | + |
| 143 | +* field - this property is query parameter which will be in search request |
| 144 | +* name - this is label for this filter |
| 145 | +* options - options which will be in select box; if it is not provided, then it will No, Yes options by default |
| 146 | + |
| 147 | +**Custom select filter** |
| 148 | + |
| 149 | +```blade |
| 150 | +<x-filter-custom-select-field> |
| 151 | + field="filter[{{ $filterField }}]" |
| 152 | + name="{{ __('Filter label') }}" |
| 153 | + options="{{__('Your option 1') }},{{ __('Your option 2')}}"> |
| 154 | +</x-filter-custom-select-field> |
| 155 | +``` |
| 156 | + |
| 157 | +* field - this property is query parameter which will be in search request |
| 158 | +* name - this is label for this filter |
| 159 | +* options - options which will be in select box |
| 160 | + |
| 161 | +**Date range filter** |
| 162 | + |
| 163 | +```blade |
| 164 | +<x-filter-date-range-field> |
| 165 | + field="filter[{{ $filterField }}]" |
| 166 | + name="{{ __('Filter label') }}"> |
| 167 | +</x-filter-date-range-field> |
| 168 | +``` |
| 169 | + |
| 170 | +* field - this property is query parameter which will be in search request |
| 171 | +* name - this is label for this filter |
| 172 | + |
| 173 | +**Input filter** |
| 174 | + |
| 175 | +```blade |
| 176 | +<x-filter-field> |
| 177 | + field="filter[{{ $filterField }}]" |
| 178 | + name="{{ __('Filter label') }}"> |
| 179 | +</x-filter-field> |
| 180 | +``` |
| 181 | + |
| 182 | +* field - this property is query parameter which will be in search request |
| 183 | +* name - this is label for this filter |
| 184 | + |
| 185 | +**Multiple select filter** |
| 186 | + |
| 187 | +```blade |
| 188 | +<x-filter-multiple-select-field |
| 189 | + field="filter[{{$filterField}}]" |
| 190 | + name="{{ __('Filter label') }}" |
| 191 | + class="{{ $eloquentModelClass }}" |
| 192 | + value="{{ $eloquentModelProperty}}"> |
| 193 | +</x-filter-multiple-select-field> |
| 194 | +``` |
| 195 | + |
| 196 | +* field - this property is query parameter which will be in search request |
| 197 | +* name - this is label for this filter |
| 198 | +* class - Eloquent model class from where will be taken values for options. For example, if class is App\Models\UserGroup, then will be rendered all user groups from `user_groups` table as options |
| 199 | +* value - property which will be shown as options. For example, if value will be `name`, column `name` from `user_groups` table will be used for option; if you want to split two columns to have them as option, put in value them separeted by comma: 'column1,column2' |
| 200 | + |
| 201 | +**Select filter** |
| 202 | + |
| 203 | +```blade |
| 204 | +<x-filter-select-field |
| 205 | + field="filter[{{$filterField}}]" |
| 206 | + name="{{ __('Filter label') }}" |
| 207 | + class="{{ $eloquentModelClass }}" |
| 208 | + value="{{ $eloquentModelProperty}}"> |
| 209 | +</x-filter-select-field> |
| 210 | +``` |
| 211 | + |
| 212 | +* field - this property is query parameter which will be in search request |
| 213 | +* name - this is label for this filter |
| 214 | +* class - Eloquent model class from where will be taken values for options. For example, if class is App\Models\UserGroup, then will be rendered all user groups from `user_groups` table as options |
| 215 | +* value - property which will be shown as options. For example, if value will be `name`, column `name` from `user_groups` table will be used for option; if you want to split two columns to have them as option, put in value them separeted by comma: 'column1,column2' |
| 216 | + |
| 217 | +**Link** |
| 218 | + |
| 219 | +```blade |
| 220 | +<x-sort-link name="{{ $fieldToSort }}"> |
| 221 | +</x-sort-link> |
| 222 | +``` |
| 223 | + |
| 224 | +* name - field to sort |
| 225 | + |
| 226 | +## Testing |
| 227 | + |
| 228 | +```bash |
| 229 | +composer test |
| 230 | +``` |
| 231 | + |
| 232 | +## PhpStan check |
| 233 | + |
| 234 | +```bash |
| 235 | +composer analyse |
| 236 | +``` |
| 237 | + |
| 238 | +## Changelog |
| 239 | + |
| 240 | +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. |
| 241 | + |
| 242 | +## Credits |
| 243 | + |
| 244 | +- [Labro Dev](https://github.com/labrodev) |
| 245 | + |
| 246 | +We are appriciate [Spatie](https://github.com/spatie) for inspiration, sharing experiences and their beaufiul Laravel packages that we are widely use and highly recommend. |
| 247 | + |
| 248 | +## License |
| 249 | + |
| 250 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
| 251 | + |
| 252 | + |
0 commit comments