I already had this "problem" when using a local scope in the same model but i used another method to reach that specific goal.
Now, i want use MeiliSearch as search engine but i have the same problem as above: the paginated result are being truncated. The pagination is set to 10. The first page has 6 results, the second one has 2 results and the fourth page has 0 results.
Article::search($this->meiliSearchText)
->query(fn($query) => ($this->showSold == "true" ? $query->whereHas('sales') : ($this->showSold == "false" ? $query->whereDoesntHave('sales') : $query)))
->paginate($this->pagination)
I think this is cause because a filter is applied AFTER the query has been retrieved. How should i treat this aspect?
EDIT:
In the documentation it's explicitly said that the "query" method should not be used for filtering result and i should use the Laravel Scout's "where" clause.
Since this callback is invoked after the relevant models have already been retrieved from your application's search engine, the query method should not be used for "filtering" results. Instead, you should use Scout where clauses.
But honestly i can't get it to work even with that useless piece:
Article::search($this->meiliSearchText)
->where(function ($query) { return $query; })
->paginate($this->pagination),
It simply kills the page returning a 500 error.
EDIT 2:
Well.
Since a search index is not a relational database, more advanced "where" clauses are not currently supported.