Home > front end >  Laravel Resource and Resource Collections
Laravel Resource and Resource Collections

Time:09-10

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
                ]
            ]
        ];

CodePudding user response:

You should use either "resource file" or "$this->map(...)", not both.

DedicatedServerResourceCollection extends JsonResource
{
    public static $wrap = 'productTypes';

    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'tagline' => $this->tagline,
            'slug' => $this->slug,
            'dedicatedServers' => DedicatedServerResource::collection($this->whenLoaded('dedicated_servers'))
        ];
    }
}

Note: Prefer to use $this->whenLoaded('dedicated_servers') instead of $this->dedicated_servers to avoid n 1 problem.

CodePudding user response:

You might want to create a different resource class for the DedicatedServer model, like SecondDedicatedServerResource.

  • Related