Skip to content

Responses

Jay edited this page May 30, 2024 · 8 revisions

For successful calls, depending on the function called, the response from the client will either be a single model (extending \SupportPal\ApiClient\Model\Model) or a \SupportPal\ApiClient\Model\Collection object, which is a collection of models.

Models

If we fetch a single user, our response will be a User model:

$user = $api->getUser(123);

Model data can be accessed as attributes on the model.

$firstname = $user->firstname; // 'John'
$lastname = $user->lastname; // 'Doe'

$formattedName = $user->formatted_name; // 'John Doe'

$active = $user->active; // 1

To retrieve all attributes on the model, you can call the toArray() function.

$data = $user->toArray();

Collections

If we fetch multiple users, our response will be a collection which contains an array of User models:

$users = $api->getUsers();

// Direct usage of collection
foreach ($users as $user) {
   ...
}

// Converting to an array first
$usersArray = $users->all();

Each model in the collection can be handled in the same way as described above.

There are two important counts available on the collection:

// The total number of records that were found for this endpoint, not considering 
// the `limit` or `start` options. Useful for pagination.
$totalCount = $users->totalCount(); // 1000

// The total number of models included in the response data.
$count = $users->count(); // 50

The collection class extends Laravel's Illuminate\Support\Collection class, thus giving access to all the same methods available such as map and filter.

Exception Handling

For API calls that fail or error for some reason, a \SupportPal\ApiClient\Exception\HttpResponseException exception is raised. The exception message will contain the error message delivered from the API.