0

I am testing Laravel with Algolia. Algolia limits the maximum number of search results to 1000. Which is fine by me, I guess.

My problem is that am not quite able to request 1000 search records from Algolia and then paginate. I tried:

$result = Model::search(request()->keywords)->take(1000)->paginate(50);

But I am still having Laravel provide more pages than the 20 that I should be having. It paginates for all the available records, if there are over 1000.

Any suggestions?

SuperOcean
  • 43
  • 4
  • The best way to use Algolia is to query it when you need the next page. Could you details why you need to get all the results at once? You will be pulling a lot of data from your database. – Julien Bourdeau Oct 13 '17 at 09:37
  • @Julien Bourdeau, I need results one page at a time. But what's right way to do it? Can you, please, point me to a post or page? – SuperOcean Oct 13 '17 at 16:53
  • Please provide more details about the end result so I can understand what your are trying to do. If you want only one result you can use the `->next()` method, although I still don't get what you're really doing. – Julien Bourdeau Oct 16 '17 at 08:14

1 Answers1

-1

I was able fix the problem by making a small change to this post by @Amigo. My code:

use Illuminate\Pagination\LengthAwarePaginator;

protected $perPage = 50;

public function search()
{

    $results = Model::search(request()->keywords)->take(1000)->get();

    $results = new LengthAwarePaginator(
       $results->slice((LengthAwarePaginator::resolveCurrentPage() *
        $this->perPage)-$this->perPage,
       $this->perPage)->all(), count($results),
       $this->perPage, null, ['path' => '']);

    return view('/searchResult', compact('results'));

}

Using the standard Laravel paginator works fine now.

SuperOcean
  • 43
  • 4