Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2 from stephenjude/develop
Browse files Browse the repository at this point in the history
Add scout query scope for full search
  • Loading branch information
stephenjude authored Jan 29, 2021
2 parents 3bdd91d + 5aa368b commit 441452b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class Post extends Model
use WithQueryFilter;
}
```
### Filter a query based on a request: /posts?column_name=search_string:
### Filter a model based on a request: /posts?column_name=search_string:
The `filter()` method is used to filter the rows in a table. The result will only include rows that meets all the criteria of the query parameters.
```php

class PostController extends Controller
Expand All @@ -37,6 +38,20 @@ class PostController extends Controller
}
}
```
### Search a model based on a request: /posts?column_name=search_string:
The `scout()` method is used to perform a full search on the model. The result for this method includes any row that meets the search criteria.
```php

class PostController extends Controller
{
public function index(Request $request)
{
// GET /posts?title=simple&slug=simple-query-filter
$posts = Post::scout($request->query())->latest()->paginate();
}
}
```

### Custom Query Parameters
You can alternatively pass an array of column names and search strings as a key-value pair to the filter method:
```php
Expand All @@ -46,6 +61,7 @@ You can alternatively pass an array of column names and search strings as a key-
];

$posts = Post::filter($queryParam)->latest()->paginate();
$posts = Post::scout($queryParam)->latest()->paginate();
```
## Column Not Found Exception
The eloquent filter scope provided in this package will throw a bad request HTTP exception if it fails to find any of the specified column names.
Expand Down
46 changes: 30 additions & 16 deletions src/WithQueryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,55 @@
trait WithQueryFilter
{
/**
* @param array $search
* @param array $filter
* @param mixed $query
*/
public function scopeFilter($query, $search)
public function scopeFilter($query, $filter)
{
/**
* Make lazy collection of search queries except the "page" key.
* Make lazy collection of filter queries except the "page" key.
* The "page" key is used for eloquent pagination request. This will
* prevent us from throwing column not found exception for the "page" key.
*/
$search = collect($search)->lazy()->except('page');
$filter = collect($filter)->lazy()->except('page');

/**
* Return query if there is no search query.
* Abort if any of the column is not found on the database tables.
*/
if ($search->isEmpty()) {
return $query;
}
$this->abortIfColumnNotFound($filter);

/**
* Abort if any of the column is not found on the database tables.
* Prepare filter query strings.
*/
$this->abortIfColumnNotFound($search);
$filter->map(function ($value, $key) use ($query) {
return $query->where($key, 'LIKE', '%'.$value.'%');
})->values()->all();
}

/**
* @param array $filter
* @param mixed $query
*/
public function scopeScout($query, $search)
{
/**
* Prepare search query strings.
* Make lazy collection of search queries except the "page" key.
* The "page" key is used for eloquent pagination request. This will
* prevent us from throwing column not found exception for the "page" key.
*/
$search = $search->map(function ($value, $key) {
return [$key, 'LIKE', '%'.$value.'%'];
})->values()->all();
$search = collect($search)->lazy()->except('page');

/**
* Apply query strings.
* Abort if any of the column is not found on the database tables.
*/
$this->abortIfColumnNotFound($search);

/**
* Prepare search query strings.
*/
return $query->where($search);
$search->map(function ($value, $key) use ($query) {
$query->orWhere($key, 'LIKE', '%'.$value.'%');
});
}

/**
Expand Down

0 comments on commit 441452b

Please sign in to comment.