Search code examples
phplaraveleloquentlaravel-6laravel-7

What is the difference between a JsonResource & ResourceCollection? in Laravel v6 or v7


Can someone explain the difference between a ResourceCollection and JsonResource?

In Laravel 6 docs you can generate 2 different types of resources... ResourceCollection and JsonResource. https://laravel.com/docs/6.x/eloquent-resources#resource-responses

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class ShopCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return parent::toArray($request);
    }
}

vs ...

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class Shop extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return parent::toArray($request);
    }
}

Solution

  • ResourceCollection is used to return a list of items, instead JsonResource is used to return a single item.

    This is a piece of code from one of my projects, where I had a list of articles and I used ResourceCollection to return an array of articles:

    <?php
    namespace App\Http\Resources\Api\Collection;
    use Illuminate\Http\Resources\Json\ResourceCollection;
    
    class AlbumCollection extends ResourceCollection
    {
        /**
         * Transform the resource collection into an array.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return array
         */
        public function toArray($request)
        {
            return $this->collection->map(function ($item) {
                return [
                    'id' => $item->id,
                    'name' => $item->name,
                    'description' => $item->description
                ];
            });
        }
    
        public function with($request)
        {
            return [
                'status' => true
            ];
        }
    }
    
    

    This code returns a list of articles. Then when the user clicks on an article, I must show to user a single article, so I must return a single article and I used the following code:

    <?php
    namespace App\Http\Resources\Api\Resources;
    use Illuminate\Http\Resources\Json\JsonResource;
    
    class NewsResource extends JsonResource
    {
        /**
         * Transform the resource into an array.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return array
         */
        public function toArray($request)
        {
            return [
                'id' => $this->id,
                'title' => $this->title,
                'body' => $this->body,
                'cover' => $this->cover,
                'view_count' => $this->view_count,
                'comment_count' => $this->comment_count,
                'like_count' => $this->like_count,
                'created_at' => $this->created_at,
                'updated_at' => $this->updated_at,
                'comments' => $this->comments
            ];
        }
    
        public function with($request)
        {
            return [
                'status' => true
            ];
        }
    }
    

    You can see the this my answer that related to your question

    I hope you find it useful.