首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel资源和资源收集

Laravel资源和资源收集
EN

Stack Overflow用户
提问于 2022-09-09 05:11:55
回答 2查看 59关注 0票数 0

我正在学习Laravel Resource API,并设置了控制器将数据传递给我的资源和资源集合。

这是服务器列表(index method),show method显示单个服务器

控制器

指数法

代码语言:javascript
复制
return new DedicatedServerResourceCollection($product->where('parent_id', 1)->with('dedicatedServers')->get());

显示方法

代码语言:javascript
复制
return new DedicatedServerResource(DedicatedServer::findOrfail($id));

我需要以不同的格式设置我的集合和资源。如何让我的资源集合循环遍历每一项并相应地格式化更改?

资源收集

代码语言:javascript
复制
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
     ];
    })
  ];

资源

代码语言:javascript
复制
     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
                ]
            ]
        ];
EN

回答 2

Stack Overflow用户

发布于 2022-09-09 22:38:08

您应该使用“资源文件”或"$this->map(...)",而不是两者兼用。

指数法

代码语言:javascript
复制
return ProductCollection::make(Product::with('dedicatedServers')->get());

ProductCollection

代码语言:javascript
复制
class ProductCollection extends ResourceCollection
{
    public function toArray($request)
    {
        return [
            'productTypes' => $this->collection,
        ];
    }
}

ProductResource

代码语言:javascript
复制
class ProductResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'tagline' => $this->tagline,
            'slug' => $this->slug,
            'dedicated_servers' => $this->whenLoaded('dedicatedServers'),
        ];
    }
}

DedicatedServerResource

代码语言:javascript
复制
class DedicatedServerResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'product_id' => $this->product_id,
            'type' => $this->type,
            'processor_line_1' => $this->processor_line_1,
            'processor_line_2' => $this->processor_line_2,
            'memory' => $this->memory,
            'storage_line_1' => $this->storage_line_1,
            'storage_line_2' => $this->storage_line_2,
            'data' => $this->data,
            'benchmark' => $this->benchmark,
            'product' => ProductResource::make($this->whenLoaded('product')),
        ];
    }
}

注意:更愿意使用$this->whenLoaded('dedicatedServers')而不是$this->dedicatedServers来避免n+1问题

票数 0
EN

Stack Overflow用户

发布于 2022-09-10 05:42:59

您可能希望为DedicatedServer模型创建一个不同的资源类,比如SecondDedicatedServerResource

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73657566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档