Laravel 11 Pagination Add Query String Example

By Hardik Savani September 4, 2024 Category : Laravel

In this post, i will show you how to automatically append query string with pagination in laravel 11 application.

Laravel makes it easy to add pagination using the `paginate()` method on a model. To show the pagination links, you can use the `links()` method. This works well for basic pagination.

However, if your page has filters (like showing only active users), you need to include the filter data in the pagination links. For example, to keep the pagination for only active users, you can add the "is_active" filter to the query string using the `appends()` method.

we will use appends() function to add automatically add query string to pagination url.

Let's see the example:

Query String URL:

http://localhost:8000/posts?is_active=1&page=1

Example:

we will use appends() method with request()->query() method to pass query string.

{{ $posts->appends(request()->query())->links('pagination::bootstrap-5') }}

I hope it can help you...

Shares