Laravel Scout, search only get 20 items

60 Views Asked by At

I'm using Laravel Scout, when I search and get the result, it only return exact 20 items, while there are actually more to get, in my case there are 56 items. How can I retrieve all items?
$products = Product::search($keyword)->get(); //this return 20 items

But when I count, there are 56 items

$products = Product::search($keyword)->count(); //this return 56 

I tried to workaround by using the paginate() method with a large number as the argument to get all results. However, it appears not possible due to performance issues and the number can be very fluctuate.

$products = Product::search($keyword)->paginate(1000000); //this is bad practice

How can I retrieve all items when search in Laravel Scout?

1

There are 1 best solutions below

0
Jeyhun Rashidov On

For scenarios where you need all results and the total count isn't very large, you could dynamically set the pagination based on the count of the results. However, be cautious with this approach due to its potential impact on performance:

$totalCount = Product::search($keyword)->count();
$products = Product::search($keyword)->paginate($totalCount);