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
Leave a reply