PHPackages                             emeguan/laravel-resources - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. emeguan/laravel-resources

ActiveLibrary[API Development](/categories/api)

emeguan/laravel-resources
=========================

Adds extra functionality to the Laravel Eloquent: API Resources, allowing you to choose attributes and relationships to return via url

0.0.10(2y ago)235MITPHPPHP &gt;=7.0.0

Since Feb 6Pushed 2y ago1 watchersCompare

[ Source](https://github.com/emeGuan/laravel-resources)[ Packagist](https://packagist.org/packages/emeguan/laravel-resources)[ RSS](/packages/emeguan-laravel-resources/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)DependenciesVersions (10)Used By (0)

Laravel API 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
------------

[](#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](https://jsonapi.org/) if we want or use one of the great libraries already available for Laravel such as:

- [Laravel Orion](https://github.com/tailflow/laravel-orion)
- [Laravel Restify](https://github.com/binarcode/laravel-restify)
- [Laravel JSON:API](https://github.com/laravel-json-api/laravel)

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](https://laravel.com/docs/9.x/eloquent-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
-----------

[](#instalation)

Install using [Composer](https://getcomposer.org)

```
composer require emeguan/laravel-resources
```

Requirements
------------

[](#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
---

[](#use)

### Model

[](#model)

```
class A extends Model
{
    use HasFactory;
    use \EmeGuan\Resources\ModelTrait;  //
