appending URL param for paging and searching

Published 3 years ago by reny410

Hi guys, I have a page with paging and multiple kind of searching feature (search by categories, users, etc).
How do I make it so that whenever a user go to another page, the URL also includes the search term?
what I mean is really just from '' to ''looking'&page=2'

in other words, whenever I click on the next page button from simplePaginate, the search term is dismissed, so I can only go to page 2 without the search term right now. I want it so if there's already a GET parameter before, don't discard it, just append, except if the name of the parameter is the same, then overwrite it. Any way I can do this 'the laravel way' ? :D

edit: nevermind. silly me, it was right there on the documentation

3 years ago (297,250 XP)

Appending To Pagination Links

You may add to the query string of pagination links using the appends method. For example, to append &sort=votes to each pagination link, you should make the following call to appends:

{!! $users->appends(['sort' => 'votes'])->render() !!}

If you wish to append a "hash fragment" to the paginator's URLs, you may use the fragment method. For example, to append #foo to the end of each pagination link, make the following call to the fragment method:

{!! $users->fragment('foo')->render() !!}

Laravel 5.1 Pagination: Displaying Results In A view Documentation

In your case it will be something like this:

{!! $users->appends(['search' => $searchTerm])->render() !!}

Also, you can check the lesson Crazy Simple Pagination, it's in Laravel 4 but the idea is the same.

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

why don't use it ? its work for me

Please sign in or create an account to participate in this conversation.