1
-
2
1
## A query database collection for use with Laravel Pipeline
3
2
4
3
[ ![ Latest Version on Packagist] ( https://img.shields.io/packagist/v/l3aro/pipeline-query-collection.svg?style=flat-square )] ( https://packagist.org/packages/l3aro/pipeline-query-collection )
@@ -25,7 +24,7 @@ $users = User::query()
25
24
->get();
26
25
```
27
26
28
- As you all can see, it's obviously that filter conditions will continue to grow as well as the duplication of same filter for other queries. We can use Laravel Pipeline combine with some pre-made queries to refactor this
27
+ As you all can see, it's obviously that filter conditions will continue to grow as well as the duplication of same filter for other queries. We can use Laravel Pipeline combine with some pre-made queries to refactor this
29
28
30
29
``` php
31
30
use Baro\PipelineQueryCollection;
@@ -42,31 +41,32 @@ $users = Users::query()->filter([
42
41
43
42
## Table of Contents
44
43
45
- * [ A query database collection for use with Laravel Pipeline] ( #a-query-database-collection-for-use-with-laravel-pipeline )
46
- * [ Table of Contents] ( #table-of-contents )
47
- * [ Installation] ( #installation )
48
- * [ Usage] ( #usage )
49
- * [ Preparing your model] ( #preparing-your-model )
50
- * [ Feature] ( #feature )
51
- * [ Bitwise filter] ( #bitwise-filter )
52
- * [ Boolean filter] ( #boolean-filter )
53
- * [ Date From filter] ( #date-from-filter )
54
- * [ Date To filter] ( #date-to-filter )
55
- * [ Exact filter] ( #exact-filter )
56
- * [ Relation filter] ( #relation-filter )
57
- * [ Relative filter] ( #relative-filter )
58
- * [ Scope filter] ( #scope-filter )
59
- * [ Trash filter] ( #trash-filter )
60
- * [ Sort] ( #sort )
61
- * [ Detector] ( #detector )
62
- * [ Custom search column] ( #custom-search-column )
63
- * [ Custom search value] ( #custom-search-value )
64
- * [ Extend filter] ( #extend-filter )
65
- * [ Testing] ( #testing )
66
- * [ Contributing] ( #contributing )
67
- * [ Security Vulnerabilities] ( #security-vulnerabilities )
68
- * [ Credits] ( #credits )
69
- * [ License] ( #license )
44
+ - [ A query database collection for use with Laravel Pipeline] ( #a-query-database-collection-for-use-with-laravel-pipeline )
45
+ - [ Table of Contents] ( #table-of-contents )
46
+ - [ Installation] ( #installation )
47
+ - [ Usage] ( #usage )
48
+ - [ Preparing your model] ( #preparing-your-model )
49
+ - [ Feature] ( #feature )
50
+ - [ Bitwise filter] ( #bitwise-filter )
51
+ - [ Boolean filter] ( #boolean-filter )
52
+ - [ Date From filter] ( #date-from-filter )
53
+ - [ Range Filter] ( #range-filter )
54
+ - [ Date To filter] ( #date-to-filter )
55
+ - [ Exact filter] ( #exact-filter )
56
+ - [ Relation filter] ( #relation-filter )
57
+ - [ Relative filter] ( #relative-filter )
58
+ - [ Scope filter] ( #scope-filter )
59
+ - [ Trash filter] ( #trash-filter )
60
+ - [ Sort] ( #sort )
61
+ - [ Detector] ( #detector )
62
+ - [ Custom search column] ( #custom-search-column )
63
+ - [ Custom search value] ( #custom-search-value )
64
+ - [ Extend filter] ( #extend-filter )
65
+ - [ Testing] ( #testing )
66
+ - [ Contributing] ( #contributing )
67
+ - [ Security Vulnerabilities] ( #security-vulnerabilities )
68
+ - [ Credits] ( #credits )
69
+ - [ License] ( #license )
70
70
71
71
## Installation
72
72
@@ -99,7 +99,9 @@ return [
99
99
```
100
100
101
101
## Usage
102
+
102
103
### Preparing your model
104
+
103
105
To use this collection with a model, you should implement the following interface and trait:
104
106
105
107
``` php
@@ -138,6 +140,7 @@ YourModel::query()->filter([
138
140
```
139
141
140
142
### Feature
143
+
141
144
Here the use all filter and sort in the collection
142
145
143
146
#### Bitwise filter
@@ -163,6 +166,7 @@ User::query()->filter([
163
166
```
164
167
165
168
#### Date From filter
169
+
166
170
``` php
167
171
use Baro\PipelineQueryCollection\DateFromFilter;
168
172
use Baro\PipelineQueryCollection\Enums\MotionEnum;
@@ -176,6 +180,7 @@ User::query()->filter([
176
180
```
177
181
178
182
#### Date To filter
183
+
179
184
``` php
180
185
use Baro\PipelineQueryCollection\DateToFilter;
181
186
use Baro\PipelineQueryCollection\Enums\MotionEnum;
@@ -188,7 +193,27 @@ User::query()->filter([
188
193
]);
189
194
```
190
195
196
+ # Range Filter
197
+
198
+ ``` php
199
+ use Baro\PipelineQueryCollection\RangeFromFilter;
200
+ use Baro\PipelineQueryCollection\RangeToFilter;
201
+
202
+ // Example: products?price_from=100&price_to=500
203
+ Product::query()->filter([
204
+ RangeFromFilter::make('price'), // Adds where price >= 100
205
+ RangeToFilter::make('price'), // Adds where price <= 500
206
+ ]);
207
+
208
+ // Example: clients?age_min=18&age_max=65
209
+ Client::query()->filter([
210
+ RangeFromFilter::make('age')->setPostFix('min'), // Adds where age >= 18
211
+ RangeToFilter::make('age')->setPostFix('max'), // Adds where age <= 65
212
+ ]);
213
+ ```
214
+
191
215
#### Exact filter
216
+
192
217
``` php
193
218
use Baro\PipelineQueryCollection\ExactFilter;
194
219
@@ -199,6 +224,7 @@ User::query()->filter([
199
224
```
200
225
201
226
#### Relation filter
227
+
202
228
``` php
203
229
use Baro\PipelineQueryCollection\RelationFilter;
204
230
@@ -223,6 +249,7 @@ User::query()->filter([
223
249
```
224
250
225
251
#### Scope filter
252
+
226
253
``` php
227
254
// users?search=Baro
228
255
@@ -246,23 +273,26 @@ User::query()->filter([
246
273
#### Trash filter
247
274
248
275
When using Laravel's [ soft delete] ( https://laravel.com/docs/master/eloquent#querying-soft-deleted-models ) , you can use the pipe ` TrashFilter `
249
- to query these models. The default query name is ` trashed ` , and filters responds to particular values:
250
- * ` with ` : the query should be ` ?trashed=with ` to include soft deleted records to the result set
251
- * ` only ` : the query should be ` ?trashed=only ` to return only soft deleted records to the result set
252
- * any other value, or completely remove ` trashed ` from request query will return only records that are not soft deleted in the result set
276
+ to query these models. The default query name is ` trashed ` , and filters responds to particular values:
253
277
254
- You can change query name ` trashed ` by passing your custom name to the ` TrashFilter ` constructor
255
- ``` php
278
+ - ` with ` : the query should be ` ?trashed=with ` to include soft deleted records to the result set
279
+ - ` only ` : the query should be ` ?trashed=only ` to return only soft deleted records to the result set
280
+ - any other value, or completely remove ` trashed ` from request query will return only records that are not soft deleted in the result set
281
+
282
+ You can change query name ` trashed ` by passing your custom name to the ` TrashFilter ` constructor
283
+
284
+ ``` php
256
285
use Baro\PipelineQueryCollection\TrashFilter;
257
286
258
287
259
288
// ?removed=only
260
289
User::query()->filter([
261
- TrashFilter::make('removed'), // where `deleted_at` is not null
290
+ TrashFilter::make('removed'), // where `deleted_at` is not null
262
291
]);
263
292
```
264
293
265
294
#### Sort
295
+
266
296
``` php
267
297
use Baro\PipelineQueryCollection\ScopeFilter;
268
298
@@ -273,7 +303,9 @@ User::query()->filter([
273
303
```
274
304
275
305
### Detector
306
+
276
307
Sometimes, you want to setup your request with a prefix like ` filter. ` . You can config every pipe that have it
308
+
277
309
``` php
278
310
use Baro\PipelineQueryCollection\ExactFilter;
279
311
@@ -285,6 +317,7 @@ User::query()->filter([
285
317
```
286
318
287
319
Or, you can define it globally
320
+
288
321
``` php
289
322
// users?filter[id]=4&filter[permission][0]=1&filter[permission][1]=4
290
323
@@ -319,7 +352,9 @@ User::query()->filter([
319
352
RelativeFilter::make('name')->value('Baro'), // where('name', 'like', "%Baro%")
320
353
]);
321
354
```
355
+
322
356
### Extend filter
357
+
323
358
Yeah, you are free to use your own pipe. Take a look at some of my filters. All of them extends ` BaseFilter ` to have some useful properties and functions.
324
359
325
360
## Testing
@@ -338,8 +373,8 @@ Please review [our security policy](../../security/policy) on how to report secu
338
373
339
374
## Credits
340
375
341
- - [ l3aro] ( https://github.com/l3aro )
342
- - [ All Contributors] ( ../../contributors )
376
+ - [ l3aro] ( https://github.com/l3aro )
377
+ - [ All Contributors] ( ../../contributors )
343
378
344
379
## License
345
380
0 commit comments