Tele oceans
Tele oceans

Reputation: 11

Laravel Meilisearch using non-case sensitive filters

I am trying to search and filter using Meilisearch and Laravel Scout.

Database Table Sample:

`

id name code
1 Panadol 123
2 Cometrex 456
3 Panadol 789

`

Data From Frontend:

Expected result:

It should return product with ID 1 because it matches BOTH search and filter.

What I have tried:

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

Answers (1)

Mansour Mehidi
Mansour Mehidi

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

Related Questions