trogne's avatar

pagination , filter lost on other pages

I have a pagination set up for all threads, with 10 threads per page.

When I filter by popular threads, when I go to other pages, I lost the popular filter.

For instance, on the first page I get the first 10 most popular threads, but when I go to page 2, it's the second page of all threads, regardless of the popular filter.

How can I have a pagination that takes into account my filtering ?

0 likes
11 replies
trogne's avatar

This is not working.

For example : http://forum.local/threads?sort=replies_count&page=6

Not sorted right.

Furthermore, I don't want to see the "sort=replies_count" in the links.

I also tried that in the filter:

$this->builder->orderBy('replies_count', 'desc')->paginate(10);

The view renders it right, but I have no longer access to the links method ($threads->links()).

rin4ik's avatar

@trogne it's obvious you have to put your own filter . provided code above was just example

1 like
trogne's avatar

I needed to pass the same filter field, not the sorting field :

{{ $threads->appends(['popular' => '1'])->links() }}

But now when I don't filter, I always have that popular field on subsequent pages... how to fix that ?

For instance : http://forum.local/threads

Hitting page 2:

http://forum.local/threads?popular=&page=2

I want the "popular" field only when present at first.

trogne's avatar

That way :

        return view('threads.index', [
            'threads' => $threads,
            'popular' => request()->has('popular')
        ]);

Then on the view :

                    @if($popular)
                        {{ $threads->appends(['popular' => '1'])->links() }}
                    @else
                        {{ $threads->links() }}                        
                    @endif

Thanks to all !

bobbybouwmann's avatar

@trogne You can't mark your own answer as the best reply here! @rin4ik gave you the perfect example, you just needed to use the correct fields! He should be the one with the credits for this one!

1 like
BernardoBF4's avatar

A bit late now, but you can use request->query() if end up having multiple filters, like this:

{{ $your_paginated_collection->appends(request()->query())->links() }}

Please or to participate in this conversation.