Skip to content

Commit aaed13c

Browse files
authored
Merge pull request #10 from adhenrique/develop
before search methods
2 parents 22a8368 + a9fad2c commit aaed13c

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

README.md

+44
Original file line numberDiff line numberDiff line change
@@ -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
134177
You can filter and paginate the data on the listing routes. To do this, send a payload on the request, using your favorite client:
135178

@@ -185,6 +228,7 @@ You can filter and paginate the data on the listing routes. To do this, send a p
185228
- [ ] Support for old Laravel versions
186229
- [ ] Or Where filter
187230
- [ ] OOP improvements
231+
- [ ] Add beforeAll and beforeFindById tests
188232

189233
## Testing
190234
```bash

src/Services/SearchService.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(SearchModel $model, FilterService $filterService)
2222

2323
public function all(Request $request)
2424
{
25-
$builder = $this->beforeSearch($this->model->query(), Auth::guard());
25+
$builder = $this->beforeAll($this->model->query(), Auth::guard());
2626

2727
$builder = $this->filterService->apply($builder, $request);
2828
$paginate = $request->get('paginate');
@@ -39,7 +39,7 @@ public function all(Request $request)
3939

4040
public function findById(int $id)
4141
{
42-
$builder = $this->beforeSearch($this->model->query(), Auth::guard());
42+
$builder = $this->beforeFindById($this->model->query(), Auth::guard());
4343

4444
return $builder->findOrFail($id);
4545
}
@@ -49,7 +49,12 @@ public function getTableName(): string
4949
return $this->model->getTable();
5050
}
5151

52-
public function beforeSearch(Builder $builder, Guard $auth): Builder
52+
public function beforeAll(Builder $builder, Guard $auth): Builder
53+
{
54+
return $builder;
55+
}
56+
57+
public function beforeFindById(Builder $builder, Guard $auth): Builder
5358
{
5459
return $builder;
5560
}

0 commit comments

Comments
 (0)