P-Torres
2 years ago

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

Posted 2 years ago by P-Torres

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.

Please sign in or create an account to participate in this conversation.