Search code examples
phplaravelsphinx

How to use Laravel pagination with Sphinx


In my search controller I use Sphinx for fetching the results. It looks like this:

    $cl = new SphinxSearch();

    $results = $cl->setMatchMode(\Sphinx\SphinxClient::SPH_MATCH_EXTENDED);
    $results = $cl->setSortMode(\Sphinx\SphinxClient::SPH_SORT_ATTR_DESC, "start");
    $results = $cl->search('@*' . $name, 'spots');
    $results = $cl->get();

It shows me a couple of hundred results and it is really heavy to load. I know that the Laravel has feature for pagination (https://laravel.com/docs/5.4/pagination), but I don't know how to use it with this Sphinx package. BTW I use this one - https://github.com/sngrl/sphinxsearch

Any suggestion?


Solution

  • You can create paginator using Sphinx results. After getting paginated results from Sphinx.

        $paginator = new LengthAwarePaginator(
            $results,
            $totalResultsCount,
            $perPage,
            $currentPage
        );
        $paginator->setCollection(collect($results));