Eduardo Güereque
Eduardo Güereque

Reputation: 67

How to append query string to pagination links using Laravel

Hello and thanks for your help. I've done some research and tried a few options but can't seem get this to work properly. I'm passing a URL with a query string into a function that loads the page via URL passed. However, I'm trying to find a way to paginate the results as well. Is there a way I can pass the query string url to Laravel's pagination links? Thanks.

My URL with query string

            <a id="searchData"class="btn btn-primary ml-1 mr-5 text-light" title="Search" type="submit" 
        onclick="ajaxLoad('{{url('dma/data')}}?startDate='+$('#startDate').val()+'&endDate='+$('#endDate').val() 
            + '&dmaNameFilter=' + encodeURI(dma_name) + '&memberNameFilter=' + encodeURI(member_name))">Search Results
        </a>

I tried this for the links():

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

I have this in my Controller:

$data = Report::where('CallDate', '>=', $start_date)->where('CallDate', '<=', $end_date)->paginate(13)->appends(request()->query());

Upvotes: 2

Views: 3956

Answers (4)

Laynor
Laynor

Reputation: 1533

Since Laravel 8, You can simply use

$paginator->withQueryString();

Upvotes: 1

Aldoo Rayan
Aldoo Rayan

Reputation: 21

Try This

$data->appends(request()->input())->links()

Upvotes: 1

Angel
Angel

Reputation: 71

You can also add this:

$this->app->resolving(\Illuminate\Pagination\LengthAwarePaginator::class, function ($paginator) {
   return $paginator->appends(Arr::except(request()->query(), $paginator->getPageName()));
});

To your AppServiceProvider

Upvotes: 4

jnbn
jnbn

Reputation: 70

You can pass any data to pagination by calling

{{ $paginator->links('view.name', ['foo' => 'bar']) }}

on your situation I think you want to pass query string to paginator; you may try

{{ $paginator->links('view.name', request()->getQueryString() ) }}

If you need to append querystrings for your ajax controller you'd better check https://github.com/spatie/laravel-query-builder

Upvotes: 0

Related Questions