18

I was wondering if it is possible to define different data for item resource and collection resource.

For collection I only want to send ['id', 'title', 'slug'] but the item resource will contain extra details ['id', 'title', 'slug', 'user', etc.]

I want to achieve something like:

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 will not work as expected because it uses PageResource so it needs

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

I could duplicate the resource into PageFullResource / PageListResource and PageFullResourceCollection / PageListResourceCollection but I am trying to find a better way to achieve the same result.

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
Dorin Niscu
  • 721
  • 1
  • 9
  • 26

3 Answers3

33

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,
                ];
            }),
        ];
    }
}
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • 3
    Would be nice if Laravel could create a `PageResource` for each item in the collection by itself. – Dees Oomens Dec 01 '17 at 13:40
  • @DeesOomens actually it does: just do `return PageResource::collection($collection);` – Kenny Horna Mar 02 '18 at 20:49
  • You can also use your Resource object inside the transform method: `return [ 'data' => $this->collection->transform(function($page){ return PageResource::make($page); }) ];` – Jona Apr 06 '20 at 10:13
  • Anyone trying to do this in Laravel 6 might hit this 'bug' https://github.com/laravel/framework/issues/32513 - The solution would be something like: `'data' => $this->collectResource($this->collection)->transform(static function ($page) { ...`. – Mere Development Jan 20 '21 at 15:22
5

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

<?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);
    }
}
cyberfly
  • 5,568
  • 8
  • 50
  • 67
  • This is the cleaner and more elegant way of writing resource and collection. This also helps maintain the resource and collection easier in the long run – Felix Labayen May 13 '20 at 18:16
2

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.

Wale
  • 1,321
  • 16
  • 11