I'm building a query for Laravel Scout.
$subchanResults = Subchan::search($query)
->when($request->input('incl_nsfw'), function ($query, $incl_nsfw) {
if ($incl_nsfw == 0) {
return $query->where('nsfw','!=', 'always');
}
}, function ($query) {
return $query->where('nsfw','!=', 'always');
})
->paginate(3);
Laravel Scout only allows basic where clauses, no advanced conditional clauses of any kind, so it's been kind of tricky so far. (Documentation: https://laravel.com/docs/8.x/scout#where-clauses)
My problem is with this line: return $query->where('nsfw','!=', 'always');
It seems that Scout will allow a simple where('column','value')
, but if I try to add the 3rd parameter to the where clauses (not equal), it doesn't work.
How can I make this query work? Or do I have to manually trim the results myself after the query?
I was hoping to find a workaround within Scout itself, but it seems that conditional clauses are just completely out of the question at the moment for scout. Here is my solution for filtering at the search engine level (Meilisearch) for the same scenario:
$subchanResults = Subchan::search($query, function ($meilisearch, $query, $options) use ($request) {
if ($incl_nsfw = $request->input('incl_nsfw')) {
$options['filters'] = 'nsfw != always';
}
return $meilisearch->search($query, $options);
})->get();