【问题标题】:Laravel 5.5 API resources for collections (standalone data)用于集合的 Laravel 5.5 API 资源(独立数据)
【发布时间】:2018-03-05 10:04:13
【问题描述】:

我想知道是否可以为项目资源和集合资源定义不同的数据。

对于收藏我只想发送['id', 'title', 'slug'],但项目资源将包含额外的详细信息['id', 'title', 'slug', 'user', etc.]

我想实现以下目标:

class PageResource extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'slug' => $this->slug,
            'user' => [
                'id' => $this->user->id,
                'name' => $this->user->name,
                'email' => $this->user->email,
            ],
        ];
    }
}

class PageResourceCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'slug' => $this->slug,
        ];
    }
}

PageResourceCollection 不会按预期工作,因为它使用 PageResource 所以它需要

return [
            'data' => $this->collection,
       ];

我可以将资源复制到 PageFullResource / PageListResourcePageFullResourceCollection / PageListResourceCollection 但我正在尝试找到一种更好的方法来实现相同的结果。

【问题讨论】:

    标签: php laravel eloquent laravel-5.5 thephpleague-fractal


    【解决方案1】:

    Resource 类上有一个集合方法。您可以将其作为 ResourceCollection 的参数输入返回,然后在集合上指定您的转换。

    控制器:

    class PageController extends Controller
    {
        public function index()
        {
            return new PageResourceCollection(PageResource::collection(Page::all()));
        }
    
        public function show(Page $page)
        {
            return new PageResource($page);
        }
    }
    

    资源:

    class PageResource extends Resource
    {
        public function toArray($request)
        {
            return [
                'id' => $this->id,
                'title' => $this->title,
                'slug' => $this->slug,
                'user' => [
                    'id' => $this->user->id,
                    'name' => $this->user->name,
                    'email' => $this->user->email,
                ],
            ];
        }
    }
    
    class PageResourceCollection extends ResourceCollection
    {
        public function toArray($request)
        {
            return [
                'data' => $this->collection->transform(function($page){
                    return [
                        'id' => $page->id,
                        'title' => $page->title,
                        'slug' => $page->slug,
                    ];
                }),
            ];
        }
    }
    

    【讨论】:

    • 如果 Laravel 可以自己为集合中的每个项目创建一个 PageResource 那就太好了。
    • @DeesOomens 实际上确实如此:只需执行return PageResource::collection($collection);
    • 你也可以在transform方法中使用你的Resource对象:return [ 'data' => $this->collection->transform(function($page){ return PageResource::make($page); }) ];
    • 任何尝试在 Laravel 6 中执行此操作的人都可能遇到此“错误”github.com/laravel/framework/issues/32513 - 解决方案类似于:'data' => $this->collectResource($this->collection)->transform(static function ($page) { ...
    【解决方案2】:

    如果您对使用链接和元数据不感兴趣,则接受的答案有效。如果需要,只需返回:

    return new PageResourceCollection(Page::paginate(10));
    

    在您的控制器中。您还应该在传递到资源集合之前急切加载其他依赖关系。

    【讨论】:

      【解决方案3】:

      如果您希望响应字段在 Resource 和 Collection 中具有相同的值,您可以在 Collection 中重用 Resource

      PersonResource.php

      <?php
      
      namespace App\Http\Resources;
      
      use Illuminate\Http\Resources\Json\Resource;
      
      class PersonResource extends Resource
      {
          /**
           * Transform the resource into an array.
           *
           * @param  \Illuminate\Http\Request  $request
           * @return array
           */
          public function toArray($request)
          {
      //        return parent::toArray($request);
      
              return [
                  'id' => $this->id,
                  'person_type' => $this->person_type,
                  'first_name' => $this->first_name,
                  'last_name' => $this->last_name,
                  'created_at' => (string) $this->created_at,
                  'updated_at' => (string) $this->updated_at,
              ];
          }
      }
      

      PersonCollection.php

      <?php
      
      namespace App\Http\Resources;
      
      use Illuminate\Http\Resources\Json\ResourceCollection;
      
      class PersonCollection extends ResourceCollection
      {
          /**
           * Transform the resource collection into an array.
           *
           * @param  \Illuminate\Http\Request  $request
           * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
           */
          public function toArray($request)
          {
      //        return parent::toArray($request);
              return PersonResource::collection($this->collection);
          }
      }
      

      【讨论】:

      • 这是编写资源和集合的更干净、更优雅的方式。从长远来看,这也有助于更轻松地维护资源和收集
      猜你喜欢
      • 2018-06-14
      • 1970-01-01
      • 2022-07-26
      • 2018-07-01
      • 2012-12-14
      • 2020-06-25
      • 2020-06-13
      • 1970-01-01
      • 2012-10-04
      相关资源
      最近更新 更多