@@ -130,6 +130,49 @@ Route::delete('dummies/{id}', 'App\Http\Controllers\DummyController@destroy');
130130```
131131
132132## 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+
133176### Searching with filters
134177You can filter and paginate the data on the listing routes. To do this, send a payload on the request, using your favorite client:
135178
0 commit comments