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);
}
}
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 “_”.
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).
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.
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.
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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With