I'm using Laravel API Resource and want to convert all parts of my instance to an array.
In my PreorderResource.php
:
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'exception' => $this->exception,
'failed_at' => $this->failed_at,
'driver' => new DriverResource(
$this->whenLoaded('driver')
)
];
}
Then to resolve:
$resolved = (new PreorderResource(
$preorder->load('driver')
))->resolve();
At first glance, the method resolve would fit it but the problem is that it doesn't work recursively. My resource resolved looks like:
array:3 [
"id" => 8
"exception" => null
"failed_at" => null
"driver" => Modules\User\Transformers\DriverResource {#1359}
]
How to resolve an API Resource to array recursively?