Download the PHP package emeguan/laravel-resources without Composer
On this page you can find all versions of the php package emeguan/laravel-resources. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-resources
Laravel API Resources
Adds extra functionality to the Laravel Eloquent: API Resources, allowing you to choose attributes and relationships to return via url
Introduction
When we create a JSON API from Laravel we can do it in many ways, the most used way (I think) is to create a Rest API. We can use the json:api specification if we want or use one of the great libraries already available for Laravel such as:
But it is possible that our API with the standard methods of a Rest API is not enough and we prefer to have extra parameters with which to perform different queries on the data before returning it in JSON format.
In this case we can generate our queries with Eloquent and return the data with the toJson method.
With the use of the Laravel Eloquent: API Resources we can control which attributes and relationships we want to return for each model in a static way. With this small library that we have here we can modify the API Resources dynamically from parameters in the url.
Instalation
Install using Composer
Requirements
I have not tested it but it should work with any version of Laravel that includes Eloquent API Resources, that is, Laravel 5.5 and later.
The code has been tested with Laravel 9
Use
Model
Controller
Call
http://laravel-resource.local/as
http://laravel-resource.local/a/1
Documentation
In the url we can specify the attributes we want from a model and the relationships to include. Let's see this with an example.
We start from these 5 models:
-
Get all As with onlly id and name attribute
http://laravel-resources.local/as?fields[as]=id,name -
Get all As include all Bs with specific attributes
http://laravel-resources.local/as?include=bs&fields[as]=id,name&fields[bs]=name - We can include multilevel relationships with the dot notation.
http://laravel-resources.local/as?include=bs.cs&fields[as]=id,name&fields[bs]=name
If attributes are not specified all are returned.
All relations are included with the dot notation, in the example it is not necessary to specify include=bs,bs.cs
-
We can include belongsTo relationships
http://laravel-resources.local/bs?fields[bs]=name,a&fields[a]=id,name -
If you need all the attributes and some relationship you can use to replace all the attributes
http://laravel-resources.local/bs?fields[bs]=,a&fields[a]=id,name -
We can include multiple relationships separated by commas
http://laravel-resources.local/as?include=bs,ds&fields[bs]=name&fields[ds]=name - You may need to create several methods that return the same collection but with different functionality. There is no problem since the name of the main relation is taken from the name of the model collection table.
http://laravel-resources.local/as-call-1?fields[as]=id,name
TODO
- When we include a second level relationship and the first level does not specify any attribute with fields[relationship]= an output similar to this is returned
http://laravel-resources.local/as?include=bs.cs&fields[as]=id&fields[bs]=&fields[cs]=name
Something similar to this could be returned where the 1st level relationship did not appear
- ...
License
Laravel API Resources is open-source software licensed under the MIT license.