- Scout Version: 9.4.6
- Scout Driver: MeiliSearch
- Laravel Version: 8.83.6
- PHP Version: 8.0.16
- Database Driver & Version: Mysql 8.0.28-0ubuntu0.20.04.3
- SDK Version (If using a third-party service): 0.23.1
Description:
This issue is related with https://github.com/laravel/scout/issues/535 and map id with key name, the solution was https://github.com/laravel/scout/commit/8a7782c5ce72b9407a3cb2e29a4865cf4efc964b creating mapIdsFrom method
The same issue is occurring with the method keys in Engine, does not take into account mapIdsFrom and call directly mapIds, which breaks the pagination count.
A solution is create keysFrom method:
/**
* Get the results of the query as a Collection of primary keys.
*
* @param \Laravel\Scout\Builder $builder
* @param string $key
* @return \Illuminate\Support\Collection
*/
public function keysFrom(Builder $builder, $key)
{
return $this->mapIdsFrom($this->search($builder), $key);
}
and modify the getTotalCount method:
/**
* Get the total number of results from the Scout engine, or fallback to query builder.
*
* @param mixed $results
* @return int
*/
protected function getTotalCount($results)
{
$engine = $this->engine();
$totalCount = $engine->getTotalCount($results);
if (is_null($this->queryCallback)) {
return $totalCount;
}
$ids = $engine->mapIdsFrom(
$results,
$key = Str::afterLast($this->model->getScoutKeyName(), '.')
)->all();
if (count($ids) < $totalCount) {
$ids = $engine->keysFrom(tap(clone $this, function ($builder) use ($totalCount) {
$builder->take(
is_null($this->limit) ? $totalCount : min($this->limit, $totalCount)
);
}), $key)->all();
}
return $this->model->queryScoutModelsByIds(
$this, $ids
)->toBase()->getCountForPagination();
}
Changing $key = Str::afterLast($this->model->getScoutKeyName(), '.')
and calling $engine->keysFrom reusing the $key
variable
Steps To Reproduce:
Using query() reproduce the bug
return Post::search($search)->query(function($query) {
$query->with('image');
})->paginate(5);
bug