Leff7's avatar
Level 4

Laravel 5.3 - Multiple pagination in a single view

I am displaying paginated results that I get from Algolia. I have two queries in my controller that I paginate. This is how I send the data from the controller to the view:

public function index(Request $request)
{
    if(!$request->q && !$request->page){
      return redirect('/');
    }

    $videos = Video::search($request->q)->paginate(1);
    $videosCollection = $videos->getCollection();

    return fractal()
      ->collection($videosCollection)
      ->parseIncludes(['player'])
      ->transformWith(new VideoTransformer)
      ->paginateWith(new IlluminatePaginatorAdapter($videos))
      ->toArray();
}

So then when I search for example for 'rene' the result is displayed on url http://videoapp.app/search?q=rene And the data that I get looks like this:

{
  data: [
    {
      id: 46,
      description: null,
      views: 0,
      created_at: "1 month ago",
      player: {
        data: {
          id: 34,
          image: "15889ca47e557c12240142_1013305815416003_5199651181367705874_n.jpg",
          first_name: "Bruno",
          last_name: "Hendricks"
    }
  }
}
],
meta: {
  pagination: {
    total: 2,
    count: 1,
    per_page: 1,
    current_page: 1,
    total_pages: 2,
    links: {
      next: "http://videoapp.app/search?query=rene&page=2"
      }
    }
  }
}

But the pagination link that is being created for the next page is "http://videoapp.app/search?query=rene&page=2" and if I click on it the metadata is changed and I get the results for the whole table and not just for the result of my first query that was 'rene' in this example. If I on the other hand type the url in the browser http://videoapp.app/search?q=rene&page=2 then it works like it should be. How I can change those generated links so that it works as it should be?

I have also tried with another example from SO, which again didn't work for me:

Controller:

    if(!$request->q && !$request->page){
      return redirect('/');
    }

    $players = Player::search($request->q)
                ->paginate(1);

    $videos = Video::search($request->q)
                ->paginate(1);
    $videos->setPageName('videos');

View

{{ $videos->appends(array_except(Request::only('q'), 'page'))->links() }}

and

{{ $players->appends(array_except(Request::only('q'), 'videos'))->links() }}

But that also didn't work, when I would click on a link for players it would go back to video results for example. Can't seem to find any solution for this problem?

0 likes
2 replies
target46k2's avatar

Laravel 5.4 Controller

function index() {
  $q1= Database\Model1::query()->paginate(10, ['*'], 'p1');
  $q2= Database\Model2::query()->paginate(20, ['*'], 'p2');
}

View Blade:

<table1>
// Datatable 1
</table1>
{{$q1->appends(['p1' => $q1->currentPage(), 'p2' => $q2->currentPage()])->links()}}


<table2>
// Datatable2
</table2>
{{$q2->appends(['p1' => $q1->currentPage(), 'p2' => $q2->currentPage()])->links()}}
2 likes
Snapey's avatar

Could you change the original query string to query instead of q so that it matches the pagination instead of the other way around?

Please or to participate in this conversation.