afonso's avatar

Paginate and eager loading when using Laravel Scout

Hi

I'm using Scout and I can't seem to be able to chain eager loading and paginate.

Lazy eager loading is working (with('address', 'address.region') doesn't seem to work att all with search):

return User::search($params['search'])->get()->load('address', 'address.region');

Pagination is working:

return User::search($params['search'])->paginate($per_page);

But I can't get both to work together. Someone that encountered this problem or can think of a workaround?

Thank you!

0 likes
2 replies
afonso's avatar

Ok. I did solve it with a custom Paginator:

            $users = User::search($params['search'])->get()->load('address', 'address.region');

            $currentPage = LengthAwarePaginator::resolveCurrentPage();

            $currentPageSearchResults = $users->slice($currentPage * $per_page, $per_page)->all();

            $paginatedSearchResults = new LengthAwarePaginator($currentPageSearchResults, count($users), $per_page);

            return $paginatedSearchResults;

Please or to participate in this conversation.