0
votes

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());
2
If you are sure request()->query() is an array with startDate/endDate / dmaNameFilter / memberNameFilter, your pagination links in blade is OK. But it seems incorrect in the controller which I think you should add more where conditions for the query, e.g. dmaNameFilter, memberNameFilter,...Phil

2 Answers

2
votes

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

0
votes

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