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

jwhm's avatar
Level 1

Using Resources on API Calls

I am trying to use a resource for filtering out information from a API call I don't need.

My Resource is called GetClientsDetails, it contains the following:

    return [
        'firstname' => $this->firstname,
		'client_id' => $this->client_id,
    ];

In my controller I am applying it to a API call's body contents.

$response = new GetClientsDetails($api->call('GetClientsDetails'));

However, it's not working correctly and returning the entire API call, instead of firstname, and client_id.

The API Call returns in DD:

{#293 ▼
  +"result": "success"
  +"userid": 53
  +"client_id": 53
  +"id": 53
  +"owner_user_id": 51
  +"uuid": "xxx"
  +"firstname": "xxx"
  +"lastname": "xxx"
  +"fullname": "xxxxxx"
  +"companyname": "xxx"
  +"email": "[email protected]"
  +"address1": "xxxx"
  +"address2": ""
  +"city": "Worksop"
  +"fullstate": "xxx"
  +"state": "xxx"
  +"postcode": "xxx"
  +"countrycode": "GB"
  +"country": "GB"
  +"phonenumber": "xxx"
  +"tax_id": ""
  +"statecode": "xxx"
  +"countryname": "United Kingdom"
  +"phonecc": 44
  +"phonenumberformatted": "+xxx"
  +"telephoneNumber": "xxx"
  +"billingcid": 0
  +"notes": ""
  +"currency": 1
  +"defaultgateway": ""
  +"cctype": "xxx"
  +"cclastfour": "xxx"
  +"gatewayid": "{"customer":"xxx","method":"xxx"}"
  +"groupid": 0
  +"status": "Active"
  +"credit": "0.00"
  +"taxexempt": false
  +"latefeeoveride": false
  +"overideduenotices": false
  +"separateinvoices": false
  +"disableautocc": false
  +"emailoptout": true
  +"marketing_emails_opt_in": false
  +"overrideautoclose": false
  +"allowSingleSignOn": 1
  +"email_verified": false
  +"language": ""
  +"isOptedInToMarketingEmails": false
  +"lastlogin": "Date: 01/01/2021 21:21<br>IP Address: xxx<br>Host: host86-186-55-17.range86-186.xxx.com"
  +"currency_code": "GBP"
}

How do I get this working?

0 likes
5 replies
automica's avatar

Can you post your whole class file for GetClientsDetails?

There’s not an awful lot in a resource file so would be interested in getting a fuller picture..

What version of laravel are you in?

jwhm's avatar
Level 1
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class GetClientsDetails extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'result' => $this->result,
            'userid' => $this->userid,
        ];
    }
}
Sergiu17's avatar

if you dd($response) - then you get all fields, if you return response()->json($response); - you should get proper response

2 likes
jwhm's avatar
Level 1

Yes, I am aware, however upon using ->json, and passing it through the Resource, I am still getting every field, not just the selected fields.

martinbean's avatar

@jwhm This isn’t what API resource classes are for.

Consider using a data mapper or something that takes the response from the third party API, and returns an object containing just the fields you’re interested in.

Please or to participate in this conversation.