Laravel Scout, search with where clausure

Viewed 1099

I just discovered Laravel Scout and I wanted to make a search with where clausure. The code shown below

$notes = Note::search($request->lecturer_search)->where([
            ['course_id','=',$course_id],
            ['course_code_number', '=', $request->course_code_number_search]
        ])->orderBy('created_at','desc')->paginate(5);

But I'm getting this error:

Type error: Too few arguments to function Laravel\Scout\Builder::where(), 1 passed in /home/vagrant/www/Bee/app/Http/Controllers/SearchController.php on line 36 and exactly 2 expected

When I remove where clausure, there is no problem.

2 Answers

I generate to queries. One via relational search. And on with scout. Then I reduce the relational results with the results of the scout search.

In your case:

$notes = Note::where([
    ['course_id','=',$course_id],
    ['course_code_number', '=', $request->course_code_number_search]
]);
$notesSearchResults = Note::search($request->lecturer_search);
$searchResultIds = $notesSearchResults->map(function ($item, $key) {
            return $item['id'];
        });
$notes = $notes->whereIn('notes.id', $searchResultIds)->get();
Related