Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

P-Torres's avatar

Mailgun, how to implement Mailgun response - pagination on a blade view?

Hi,

I'm working with Mailgun's API, I'm trying to implement pagination when displaying the received data.

I have the following response from Mailgun.

{#383 ▼
  +"http_response_body": {#381 ▼
    +"items": array:10 [▶]
    +"paging": {#382 ▼
      +"first": "https://api.mailgun.net/v2/lists/[email protected]/members/pages?page=first&limit=10"
      +"last": "https://api.mailgun.net/v2/lists/[email protected]/members/pages?page=last&limit=10"
      +"next": "https://api.mailgun.net/v2/lists/[email protected]/members/pages?page=next&address=email-address-a%40domain.com&limit=10"
      +"previous": "https://api.mailgun.net/v2/lists/[email protected]/members/pages?page=prev&address=email-adress-b%40domain.com&limit=10"
    }
  }
  +"http_response_code": 200
}

On my controller I have the following:

public function members($address)
    {
        # Instantiate the client.
        $mgClient = new Mailgun(env('MAILGUN_SECRET'));

        # Issue the call to the client.
        $result = $mgClient->get("lists/$address/members/pages", array(
            'limit'      =>  10
        ));

        return view('dash.members.show', compact('result'));
    }

My view looks like this:

<div class="table-responsive">
    <table class="table table-striped table-hover dev-table-row-vertical-middle">
        <thead>
        <tr>
            <th>Name</th>
            <th>E-mail Address</th>
            <th>Subscribed</th>
        </tr>
        </thead>
        <tbody>
            @foreach($result->http_response_body->items as $obj)
                <tr>
                    <td>{{ $obj->name }}</td>
                    <td>{{ $obj->address }}</td>
                    <td>
                        @if($obj->subscribed==true)
                            Yes
                        @else
                            No
                        @endif
                    </td>
                </tr>
            @endforeach
        </tbody>
    </table>
</div>

<ul class="pagination">
    <li><a href="{{ $result->http_response_body->paging->first }}">First</a></li>
    <li><a href="{{ $result->http_response_body->paging->previous }}">Previous</a></li>
    <li><a href="{{ $result->http_response_body->paging->next }}">Next</a></li>
    <li><a href="{{ $result->http_response_body->paging->last }}">Last</a></li>
</ul>
    

Can anyone guide me on how the code needs to be in order for the pagination to work?

I have tried many different approaches, I feel first I need to understand what needs to happen, anyone?

Thank you.

0 likes
1 reply
andrewclark's avatar

One approach but not the most efficient would be to get all results via your API request and not limit them to ten. Then you can pass all the results into the LengthAwarePaginator class.

This will get you the results you're looking for but by no means the best way to do it!

public function member($address, $page = null) {
    $result = $mgClient->get("lists/$address/members/pages", []);

    $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
    $items = Collection::make($result->items);
    $result = new LengthAwarePaginator($items->forPage($page, 10), $items->count(), 10, $page, []);

    return view('dash.members.show', compact('result'));
}

Your front end would look more like:

<table>
    @foreach ($result as $obj)
         <tr>
            <td>{{ $obj->name }}</td>
            <td>{{ $obj->address }}</td>
            <td>
                {{ $obj->subscribed ? 'Yes' : 'No' }}
            </td>
        </tr>
    @endforeach
</table>

// Laravels default Pagination links template in Bootstrap
{{ $result->links() }}

You may wish to query the API each time a new page is loaded (depends how many results you are expecting!) and then there would be no need for a pagination class, you'd just modify your API query based upon the page parameter that's provided. If this doesn't help let me know and I'll write up a more thorough answer.

Be sure to check https://laravel.com/api/5.4/Illuminate/Pagination/LengthAwarePaginator.html

Please or to participate in this conversation.