3

I am using shift31/laravel-elasticsearch:~1.0. I want to implement pagination on my list page.

Search code :

   $params = [
        'index' => 'my_index',
        'type' => 'product',
        'body' =>  [
                       'query'=>[
                                 'match'=>[
                                           'category'=>$category
                                          ]
                                 ]
                   ];
   ];
  $response = \Es::Search($params);

How to use pagination in above query ?

Update :

I used from and size in query but how to give pagination link in view page ? and how to update from by click on page ?

7
  • Have you looked in the docs at all? elastic.co/guide/en/elasticsearch/reference/current/… elastic.co/guide/en/elasticsearch/guide/current/pagination.html Commented Mar 1, 2016 at 10:04
  • OK thank you james. But how to give link on view page and what if i click on 2nd 3rd page of pagination link ? If i give 0 to 10 on first time. Commented Mar 1, 2016 at 10:10
  • Actually, have you tried paginating the results with the built in paginator. Try using $response = \Es::Search($params)->paginate(10); And then after passing this to your view using $response->render() to generate the paginator. Commented Mar 1, 2016 at 10:22
  • No I didn't use that. OK thanks let me try that one. Commented Mar 1, 2016 at 10:39
  • let me know how it goes Commented Mar 1, 2016 at 10:41

1 Answer 1

10

in repository

 $params = [
        'index' => 'my_index',
        'type' => 'product',
          'size' = $per_page;
          'from' = $from;
        'body' =>  [
                       'query'=>[
                                 'match'=>[
                                           'category'=>$category
                                          ]
                                 ]
                   ];
   ];

$response = \Es::Search($params);
 $access = $response['hits'];
        return $access;

i set $per_page and $from in controller

$per_page = $request->get('limit', 10);
    $from = ($request->get('page', 1) - 1) * $per_page;
    $access = $this->repository->Index($per_page, $from);

    $admin_exceptions = new LengthAwarePaginator(
            $access['hits'],
            $access['total'],
            $per_page,
            Paginator::resolveCurrentPage(),
            ['path' => Paginator::resolveCurrentPath()]);
 return view('adminexception.index', compact('admin_exceptions'))->withInput($request->all());

and now use render in in the views {{!!$admin_exceptions->render()!!}}

Sign up to request clarification or add additional context in comments.

6 Comments

can please share you LengthAwarePaginator() function >?
Sorry, can you please share your LengthAwarePaginator() function ?
Are you using php and larvel?
I am using laravel 5.1
LengthAwarePaginator() is a prebuilt function in laravel 5.1 package.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.