PHPackages                             jfadich/eloquent-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. jfadich/eloquent-resources

ActiveLibrary[API Development](/categories/api)

jfadich/eloquent-resources
==========================

Easily generate JSON responses for Laravel 5.1+

v1.1(5y ago)35183[2 issues](https://github.com/jfadich/eloquent-resources/issues)MITPHPPHP &gt;=7.3CI failing

Since Jun 20Pushed 5y ago1 watchersCompare

[ Source](https://github.com/jfadich/eloquent-resources)[ Packagist](https://packagist.org/packages/jfadich/eloquent-resources)[ RSS](/packages/jfadich-eloquent-resources/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)Dependencies (6)Versions (17)Used By (0)

Eloquent Resources
==================

[](#eloquent-resources)

Eloquent resources provides Laravel integration for the [thephpleague/Fractal](http://fractal.thephpleague.com/) package.

Fractal utilizes transformers to add a layer between your database schema and your API responses. Each model should have a transformer to define the how it should be represented in your API.

Eloquent resources integrates Fractal wil Laravel and adds a few helper methods to help transform models. It also adds the artisan command to quickly generate new transformers. When nested relationships are requested to be included It can also automatically eager load fractal includes and parse the parameters to apply the proper `sort` and `order`query constraints.

Fractal can be verbose. This is the code to create transformed data using an eloquent model and a fractal transformer.

```
 public function lastesPost()
 {
    $fractal = new League\Fractal\Manager();
    $post = App\Post::latest()->first();
    $transformer = new App\Transformers\PostTransformer();
    $resource = League\Fractal\Resource\Item($post, $transformers);

    $data = $fractal->createData($resource, $transformer)->toArray();

    return response()->json($data);
 }

```

Eloquent Resources will handle the Fractal manager for you.

```
public function lastesPost()
{
    $post = Post::latest()->first();

    return $this->respondWithItem($post);
}

```

Installation
------------

[](#installation)

Install the package using composer.

```
$ composer require jfadich/eloquent-resources

```

Add the service provider to the `$providers` array in `app.php`

```
jfadich\EloquentResources\Providers\LaravelServiceProvider::class

```

If you wish to make and customizations publish the config config to `config/eloquent-resources.php` with this command.

```
$ php artisan vendor:publish --provider="jfadich\EloquentResources\Providers\LaravelServiceProvider"

```

### Add Eloquent Resources to your controller

[](#add-eloquent-resources-to-your-controller)

Update your base controller in simply add the `RespondsWithResources` trait to your base controller or any controller that you with to use Eloquent Resources.

```
