I'm learning the Laravel Resource API and have setup my controller to pass the data to my Resource and my Resource Collection.
This is the list of servers (index method) with the show method showing the individual server
Controller
Index method
return new DedicatedServerResourceCollection($product->where('parent_id', 1)->with('dedicatedServers')->get());
Show Method
return new DedicatedServerResource(DedicatedServer::findOrfail($id));
I need to format my collection and resource differently. How can I get my Resource Collection to loop through each item and format the changes accordingly?
Resource Collection
return [
'productTypes' => $this->map(function($data){
return [
'id' => $data->id,
'title' => $data->title,
'tagline' => $data->tagline,
'slug' => $data->slug,
'dedicatedServers' => DedicatedServerResource::collection($this->resource)
// I need to pass 'dedicatedServers' === $this->dedicated_servers
];
})
];
Resource
return [
'id' => $this->id,
'productId' => $this->product_id,
'type' => $this->type,
'price' => $this->price,
'config' => [
'processorLine1' => $this->processor_line_1,
'processorLine2' => $this->processor_line_2,
'memory' => $this->memory,
'storageLine1' => $this->storage_line_1,
'storageLine2' => $this->storage_line_2,
'data' => $this->data,
'benchmark' => [
'benchmark' => $this->benchmark,
'benchmarkPercentage' => $this->benchmark_percentage
]
]
];