Laravel 5.5 API resources for collections (standalone data) Laravel 5.5 API resources for collections (standalone data) laravel laravel

Laravel 5.5 API resources for collections (standalone data)


The Resource class has a collection method on it. You can return that as the parameter input to your ResourceCollection, and then specify your transformations on the collection.

Controller:

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

Resources:

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


If you want the response fields to have the same value in the Resource and Collection, you can reuse the Resource inside the Collection

PersonResource.php

<?phpnamespace 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

<?phpnamespace 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);    }}


The accepted answer works, if you are not interested in using links and meta data. If you want, simply return:

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

in your controller. You should also look to eager load other dependent relationships before passing over to the resource collection.