Reputation: 11
I am trying to search and filter using Meilisearch and Laravel Scout.
`
id | name | code |
---|---|---|
1 | Panadol | 123 |
2 | Cometrex | 456 |
3 | Panadol | 789 |
`
query = pana
(It is a string for search and it will search for all columns)filters[['column' => 'code', 'value' => '23']
(It is not mandatory to send filters but user may use filters aligned with search)It should return product with ID 1 because it matches BOTH search and filter.
I have integrated Meilisearch and indexed all my model data and used the search method.
public function search(Request $request) { return tap(Product::search($request->q)->paginate(10), function ($q) { return $q->load('category'); }); }
How can I add the filter to the search function? I have tried to use where
but it is case-sensitive means that user should type the entire keyword for filtering.
Upvotes: 0
Views: 420
Reputation: 21
$filters = $this->filters();
$results = Product::search(
$query,
function (Indexes $meiliSearch, string $query, array $options) use ($filters)
{
$options['filter'] = $filters;
return $meiliSearch->search($query, $options);
}
);
protected function filters()
{
if (request('name'))
{
$options ? $options .= ' AND name =' . request('name') : $options .= 'name
=' . request('name');
}
return $options;
}
Upvotes: 0