Laravel 5.5 用于集合的 API 资源(独立数据)

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

对于收集,我只想发送,但项目资源将包含额外详细信息['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,
       ];

我可以将资源复制到/和/中,但我正在努力寻找更好的方法来获得相同的结果。PageFullResourcePageListResourcePageFullResourceCollectionPageListResourceCollection


答案 1

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

控制器:

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,
                ];
            }),
        ];
    }
}

答案 2

如果希望响应字段在“资源”和“集合”中具有相同的值,则可以在“集合”中重用“资源”

人员资源.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,
        ];
    }
}

人员集合.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);
    }
}

推荐