Leff7's avatar
Level 4

Laravel - Displaying paginated results in tabs content

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) {
            return redirect('/');
        }

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

        return view('search.index', [
            'players' => $players,
            'videos' => $videos,
        ]);
    }

And then in the controller I am displaying the results in tabs.

                  <div class="search-list panel-body">
                      <ul class="nav nav-tabs" role="tablist">
                        @if ($videos->count())
                          <li role="presentation" class="active"><a href="#videos" aria-controls="videos" role="tab" data-toggle="tab">Videos <span class="badge">{{$videos->count()}}</span></a></li>
                        @endif
                        @if ($players->count())
                          <li role="presentation"><a href="#players" aria-controls="players" role="tab" data-toggle="tab">Players <span class="badge">{{$players->count()}}</span></a></li>
                        @endif
                      </ul>

                      <!-- Tab panes -->
                      <div class="tab-content">
                        @if ($videos->count())
                            <div role="tabpanel" class="tab-pane active" id="videos">
                              @foreach ($videos as $video)
                                  @include ('video.partials._video_result', [
                                      'video' => $video
                                  ])
                              @endforeach
                              {{ $videos->links() }}
                            </div>

                        @endif
                        @if ($players->count())
                            <div role="tabpanel" class="tab-pane" id="players">
                              @foreach ($players as $player)
                                  @include ('video.partials._player_result', [
                                      'player' => $player
                                  ])
                              @endforeach
                              {{ $players->links() }}
                            </div>

                        @endif
                      </div>
                      @if(!$videos->count() && !$players->count())
                          <p>No videos or players found.</p>
                      @endif
                    </div>
                </div>

But, it is not working as I expected. The pagination links are not working, when I try to navigate them I get redirected to home page. Not sure what to do about that, to have them function?

I tried to achieve this by setting the queries like this:

$players = Player::search($request->q)->paginate(1, ['*'], 'players');
$videos = Video::search($request->q)->paginate(1, ['*'], 'videos');

But I get an error:

AlgoliaException in Client.php line 921: Invalid value for "page" parameter, expected integer >= 0
0 likes
0 replies

Please or to participate in this conversation.