Skip to content

Commit e3c34cd

Browse files
committed
Initial commit
0 parents  commit e3c34cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2011
-0
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 4
7+
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.{yml,yaml}]
15+
indent_size = 2

.gitattributes

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Path-based git attributes
2+
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
3+
4+
# Ignore all test and documentation with "export-ignore".
5+
/.github export-ignore
6+
/.gitattributes export-ignore
7+
/.gitignore export-ignore
8+
/phpunit.xml.dist export-ignore
9+
/art export-ignore
10+
/docs export-ignore
11+
/tests export-ignore
12+
/workbench export-ignore
13+
/.editorconfig export-ignore
14+
/.php_cs.dist.php export-ignore
15+
/psalm.xml export-ignore
16+
/psalm.xml.dist export-ignore
17+
/testbench.yaml export-ignore
18+
/UPGRADING.md export-ignore
19+
/phpstan.neon.dist export-ignore
20+
/phpstan-baseline.neon export-ignore

.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.idea
2+
.phpunit.cache
3+
build
4+
composer.lock
5+
coverage
6+
docs
7+
phpunit.xml
8+
phpstan.neon
9+
testbench.yaml
10+
vendor
11+
node_modules

CHANGELOG.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Changelog
2+
3+
All notable changes to `labrodev/laravel-filter-components`
4+
documented in this file.
5+
6+
## [1.00] - 2023-11-18
7+
8+
### Added
9+
10+
- View components:
11+
12+
* Boolean filter
13+
* Custom select filter
14+
* Date range filter
15+
* Input filter
16+
* Multiple select field
17+
* Select field
18+
* Sort link
19+
20+
- QueryBuilder classes:
21+
22+
* DateRangeFilter
23+
* IsNotNullFilter
24+
* WhereInFilter
25+

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 labrodev
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Labro Dev <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+252
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
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

Comments
 (0)