stretch0's avatar

Simple pagination next / previous URL's with existing query string.

I am trying to use simple pagination as well as sending a GET request with a query string. This works fine and results are being returned normally however, the next and previous URL's are generated without the GET query.

For example, my GET request may look something like this:

http://dash.app/adcampaign/?page=1&campaign_status=active

but the next URL returned is:

http://dash.app/adcampaign/?page=2"

Am I best to use a custom pagination function or is there a way Laravel handles this kind of issue?

0 likes
5 replies
thomaskim's avatar

@stretch0 When you render your pagination links, I believe you can achieve do that by doing this:

{!! $paginationCollection->appends(['campaign_status' => 'active'])->render() !!}

Edit: Looks like bestmomo already mentioned it. :)

bobbybouwmann's avatar

Well you can do something like this

route('my_route', array_merge(['page' => 2, Request::all())

So basically you get all the url parameters and add them to the route.

stretch0's avatar

@bestmomo & @thomaskim the problem with that is, I am not using blade templating. I am actually building this as an API and the front end will be done in a JS framework.

Surely there is some way I can apply the same logic in the controller though. Thanks for the suggestion

stretch0's avatar

Have found a solution (albeit not that glamorous) but if any one is interested:

    // overrides simplePagination to include existing query strings
    $nextUrl = '';
    $prevUrl = '';
    foreach( Request::all() as $field => $query ){

        if( $field !== 'page' ) {

            $nextUrl .= '&' . $field . '=' . $query ;    
            $prevUrl .= '&' . $field . '=' . $query ;
        
        }

    }

    // if there are next or previous pages then set URL
    if( ! is_null( $ad_campaigns['next_page_url'] ) ){

        $ad_campaigns['next_page_url'] .= $nextUrl;    

    }

    if( ! is_null( $ad_campaigns['prev_page_url'] ) ){

        $ad_campaigns['prev_page_url'] .= $prevUrl;

    }

    // end of pagination override

Please or to participate in this conversation.