What is the difference between a JsonResource & ResourceCollection? in Laravel v6 or v7 [duplicate]
Asked Answered
M

2

7

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);
    }
}
Mathildamathilde answered 3/4, 2020 at 22:12 Comment(1)
A collection contains multiple items. A resource is a single item. It's like a bag of apples vs one apple.Shearwater
L
13

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.

Lund answered 3/4, 2020 at 23:33 Comment(0)
B
6

COLLECTION is for multiple RESOURCES.

I'm used to do this way, for example:

ClientEntityCollection:

public function toArray($request)
{
    return [
        'data' => $this->collection->map(function ($row) use ($request) {
            return (new ClientEntityResource($row))->toArray($request);
        })
    ];
}

ClientEntityResource

public function toArray($request)
{
    $data =  [
        'type' => 'cliententity',
        'id' => $this->clienteEntidadeId,
        'clienteEntidadeNome' => $this->clienteEntidadeNome,
    ];

    return $data;
}

Then, in my controller I can call on a full list:

return (new ClientEntityCollection($rows))
        ->response()
        ->setStatusCode(200);

Or a single item:

return (new ClientEntityResource($row))
        ->response()
        ->setStatusCode(200);

It works like a charm, with pagination and all..

Bopp answered 4/4, 2020 at 5:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.