2

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());
1
  • 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,... Commented Sep 28, 2019 at 0:56

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

Try This

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

Comments

1

Since Laravel 8, You can simply use

$paginator->withQueryString();

Comments

0

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

1 Comment

Awesome! I'll give this a try. Thank you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.