Posts Learn Components Snippets Categories Tags Tools About
/

How to Pass Query String in Laravel Pagination?

Learn how to pass query string in Laravel Pagination using the provided helper method

Created on Dec 04, 2021

637 views

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

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

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that are right for your project.

View Components

Sponsors 👑

+ Add Yours
)