Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a json resource & resource collection? in Laravel

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);
    }
}
like image 391
Josh Avatar asked Oct 10 '19 04:10

Josh


People also ask

What is resource in JSON?

Resources are modeled as a JSON object. The type of the resource is stored under the special key:value pair “_type”. Data associated with a resource is modeled as key:value pairs on the JSON object. To prevent naming conflicts with internal key:value pairs, keys must not start with “_”.

What is the difference between a resource and a representation in rest?

The representation of a resource is the way that your service communicates the state of the resource, e.g. XML, JSON that represents the state of the lamp. In REST API, a resource is identified by a uniform identifier (e.g. URI).

What is meant by a resource in REST?

A resource in REST is a similar Object in Object Oriented Programming or is like an Entity in a Database. Once a resource is identified then its representation is to be decided using a standard format so that the server can send the resource in the above said format and client can understand the same format.

What is representation in API?

A representation of a resource is a document of a certain media type, such as HTML or JSON. The FotoWeb REST API defines multiple JSON-based media types. Each resource can have one or more representations and at most one representation of each media type.

What is json JSON?

JSON stands for JavaScript Object Notation. JSON is a lightweight format for storing and transporting data. JSON is often used when data is sent from a server to a web page. JSON is "self-describing" and easy to understand

How do I know if a resource is JSON?

Consequently, the server will return the resource represented as JSON. The response will contain the Content-Type header with the application/json value, indicating the content of the response is a JSON. Besides JSON and XML, for example, the resources can be represented as images or videos.

What is the difference between JSON and XML?

JSON (JavaScript Object Notation) is a lightweight data-interchange format and it completely language independent. It is based on the JavaScript programming language and easy to understand and generate. XML (Extensible markup language) was designed to carry data, not to display data. It is a W3C recommendation.

How many objects are there in JSON?

It contains three objects. Each object is a record of a person (with a first name and a last name). A common use of JSON is to read data from a web server, and display the data in a web page. For simplicity, this can be demonstrated using a string as input.


Video Answer


1 Answers

When you are converting a single model to json, that is a json resource, when you are converting a collection of model to json, that is resource collection.

simply If you are returning a collection of resources or a paginated response that is a collection.

See documentation

to generating resources that transform individual models, you may generate resources that are responsible for transforming collections of models. This allows your response to include links and other meta information that is relevant to an entire collection of a given resource.

like image 178
Prafulla Kumar Sahu Avatar answered Sep 17 '22 15:09

Prafulla Kumar Sahu