@@ -130,6 +130,49 @@ Route::delete('dummies/{id}', 'App\Http\Controllers\DummyController@destroy');
130
130
```
131
131
132
132
## Using
133
+ ### Before Search filters
134
+ In the SearchService class you have two methods that help you to pre-start queries according to your needs: ` beforeAll ` and` beforeFindById ` .
135
+ Each method receives 2 parameters: ` builder ` with the Eloquent instance started and ` auth ` , with the user session - if are logged in.
136
+ You just need to override the methods, but ensure that the return is eloquent's ` Builder ` . Look:
137
+ ``` php
138
+ class DummySearchService extends SearchService
139
+ {
140
+ protected SearchModel $model;
141
+ protected FilterService $filterService;
142
+
143
+ public function __construct(DummySearchModel $model, DummyFilterService $filterService)
144
+ {
145
+ $this->model = $model;
146
+ $this->filterService = $filterService;
147
+ }
148
+
149
+ public function beforeAll(Builder $builder, Guard $auth): Builder
150
+ {
151
+ return $builder;
152
+ }
153
+
154
+ public function beforeFindById(Builder $builder, Guard $auth): Builder
155
+ {
156
+ return $builder;
157
+ }
158
+ }
159
+ ```
160
+ In my use case, logged in as admin, I usually filter from the list of users my own user. Look:
161
+ ``` php
162
+ // ...
163
+ public function beforeAll(Builder $builder, Guard $auth): Builder
164
+ {
165
+ return $this->removeLoggedFromSearches($builder, $auth);
166
+ }
167
+
168
+ private function removeLoggedFromSearches($builder, $auth)
169
+ {
170
+ $id = $auth->id();
171
+ return $builder->where('id', '<> ', $id);
172
+ }
173
+ ```
174
+
175
+
133
176
### Searching with filters
134
177
You can filter and paginate the data on the listing routes. To do this, send a payload on the request, using your favorite client:
135
178
@@ -185,6 +228,7 @@ You can filter and paginate the data on the listing routes. To do this, send a p
185
228
- [ ] Support for old Laravel versions
186
229
- [ ] Or Where filter
187
230
- [ ] OOP improvements
231
+ - [ ] Add beforeAll and beforeFindById tests
188
232
189
233
## Testing
190
234
``` bash
0 commit comments