Apr 3, 2020
0
Level 1
Transform third party API response in collection
Hello everyone,
I'm using a third party API to generate some billing information using a Job and now, I need to be able to concatenate the response of this API and some local data. Thus, I'm trying to achieve it using resources, but it is giving the following error: "Call to a member function first() on string" when I'm calling it as return BoletoResource::collection(json_decode($response->getBody(),true)['Billing']);. Could someone help me?
class BillingResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'IdBilling' => $this->resource['IdBoleto'],
'DueDate' => $this->resource['DueDate'],
'PaymentDate' => $this->resource['PaymentDate'],
'CreditDate' => $this->resource['CreditDate'],
'PrincipalValue' => $this->resource['PrincipalValue'],
'InterestValue' => $this->resource['InterestValue'],
'FineValue' => $this->resource['FineValue'],
'TotalValue' => $this->resource['TotalValue'],
'PaidValue' => $this->resource['PaidValue'],
'Status' => $this->resource['Status'],
'MailToSend' => $this->resource['MailToSend'],
'PhoneToSend' => $this->resource['PhoneToSend'],
'Comments' => $this->resource['Comments'],
'OurNumber' => $this->resource['OurNumber'],
'Identifier' => $this->resource['Identifier'],
'ReturnMessage' => $this->resource['ReturnMessage'],
'ReturnCode' => $this->resource['ReturnCode'],
'Barcode' => $this->resource['Barcode'],
'DigitableLine' => $this->resource['DigitableLine'],
'OccurrenceMotive' => $this->resource['OccurrenceMotive'],
'Url' => $this->resource['Url'],
'CustomerName' => $this->resource['CustomerName'],
'CustomerTaxNumber' => $this->resource['CustomerTaxNumber'],
'PayerBankCode' => $this->resource['PayerBankCode'],
'PayerBankBranchCode' => $this->resource['PayerBankBranchCode']
];
}
}
class BillingCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => $this->collection
->map
->toArray($request)
->all()
];
}
}
Please or to participate in this conversation.