0

I am working with laravel vue and algolia. Everything working fine except the pagination properly. The pagination is working but only it shows the fisrt page result. When i click 2,3... pages link of pagination button it doesn't fetch the next page result. What i have done are given below:

SearchController.php

public function search(Request $request)
{
    $error = ['error' => 'No results found, please try with different keywords.'];

    if($request->has('q')) {
        $movies = Movie::search($request->get('q'))->get();
        return $movies ? $movies : $error;
    }

    return $error;
}

Pagination.js

var search = instantsearch({
    appId: 'myid',
    apiKey: 'mykey',
    indexName: 'myindex',
    urlSync: true
});

search.addWidget(
    instantsearch.widgets.pagination({
      container: '#pagination-container',
      maxPages: 20,
      scrollTo: false
    })
);

search.start();
Wahidul Alam
  • 1,216
  • 4
  • 26
  • 56

1 Answers1

2

I think you need to use the paginate method to send the correct page to the frontend.

public function search(Request $request)
{
    $error = ['error' => 'No results found, please try with different keywords.'];

    if($request->has('q')) {
        $movies = Movie::search($request->get('q'))->paginate(20)->all();
        return $movies ? $movies : $error;
    }

    return $error;
}

You can pass the page number explicitly if you need to:

Movie::search($request->get('q'))->paginate(20, 'p', 3)->all();
Julien Bourdeau
  • 1,193
  • 11
  • 17
  • I am using vue js for asynchronous data transmission and also instantsearch paginate. Not using Laravel paginate. Can you help me to achieve this pagination. when i click on pagination button the url becomes like this : http://127.0.0.1:8000/?q=&idx=movies&p=2&is_v=1 . but it doesnt bring anything for the next page. – Wahidul Alam May 11 '17 at 18:42
  • So you want to search on the backend, pass all the results to your frontend and let javascript show or hide the correct page? Instantsearch is designed to query algolia directly from the client side, at each page you'll create a new request to algolia server and you'll get just the page you need. – Julien Bourdeau May 12 '17 at 07:45