Home / Snippets / How to Pass Query String in Laravel Pagination?
How to Pass Query String in Laravel Pagination? cover

How to Pass Query String in Laravel Pagination?

2.1K

2 years ago

0 comments

To pass the query string in Laravel Pagination you can make use of the provided helper method by Laravel. Imagine you are paginating posts and your query is like the following.
<?php

$posts = Post::paginate(20);
To get the pagination links you can call the following methods on your blade files.
{{ $posts->links('pagination::tailwind') }}

Laravel withQueryString Helper Method


Now to pass the query string in Laravel pagination you can use the "withQueryString()" method. 
withQueryString()
This is quite straight forward and you just have to chain it before the "links()" method.
{{ $posts->withQueryString()->links('pagination::tailwind') }}
Do note that if you have 10 paginations on 10 different pages then you will have to call the function on each of the pages. To register or specify the query strings globally you can call register in the AppServiceProvider itself so it's available for all.
<?php

$this->app->resolving(LengthAwarePaginator::class, function ($paginator) {
    return $paginator->appends(array_except(Input::query(), $paginator->getPageName()));
});

Other Reads

notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this