2017 © Pedro Peláez
 

library laravel-fractal

An easy to use Fractal integration for Laravel applications

image

spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  • Thursday, July 19, 2018
  • by Spatie
  • Repository
  • 32 Watchers
  • 1130 Stars
  • 964,338 Installations
  • PHP
  • 54 Dependents
  • 1 Suggesters
  • 111 Forks
  • 1 Open issues
  • 53 Versions
  • 14 % Grown

The README.md

An easy to use Fractal wrapper built for Laravel and Lumen applications

Latest Version on Packagist run-tests Total Downloads, (*1)

The package provides a nice and easy wrapper around Fractal for use in your Laravel applications. If you don't know what Fractal does, take a peek at their intro. Shortly said, Fractal is very useful to transform data before using it in an API., (*2)

Using Fractal data can be transformed like this:, (*3)

use League\Fractal\Manager;
use League\Fractal\Resource\Collection;

$books = [
   ['id' => 1, 'title' => 'Hogfather', 'characters' => [...]],
   ['id' => 2, 'title' => 'Game Of Kill Everyone', 'characters' => [...]]
];

$manager = new Manager();

$resource = new Collection($books, new BookTransformer());

$manager->parseIncludes('characters');

$manager->createData($resource)->toArray();

This package makes that process a tad easier:, (*4)

fractal()
   ->collection($books)
   ->transformWith(new BookTransformer())
   ->includeCharacters()
   ->toArray();

Lovers of facades will be glad to know that a facade is provided:, (*5)

Fractal::collection($books)->transformWith(new BookTransformer())->toArray();

There's also a very short syntax available to quickly transform data:, (*6)

fractal($books, new BookTransformer())->toArray();

You can transform directly from a Laravel collection as well:, (*7)

collect($books)->transformWith(new BookTransformer());

Transforming right from a Laravel collection is particularly useful for Eloquent results:, (*8)

Users::all()->transformWith(new UserTransformer())->toArray();

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website., (*9)

Support us

, (*10)

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products., (*11)

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall., (*12)

Installation in Laravel 5.5 and up

You can pull in the package via composer: ``` bash composer require spatie/laravel-fractal, (*13)


The package will automatically register itself. If you want to [change the default serializer](https://github.com/spatie/fractalistic#changing-the-default-serializer), the [default paginator](https://github.com/spatie/fractalistic#using-pagination), or the default fractal class `Spatie\Fractal\Fractal` you must publish the config file: ```bash php artisan vendor:publish --provider="Spatie\Fractal\FractalServiceProvider"

If you're upgrading to Laravel 5.5, the existing config file should be renamed from laravel-fractal.php to fractal.php, (*14)

This is the contents of the published file:, (*15)

return [
     /*
     * The default serializer to be used when performing a transformation. It
     * may be left empty to use Fractal's default one. This can either be a
     * string or a League\Fractal\Serializer\SerializerAbstract subclass.
     */
    'default_serializer' => '',

    /* The default paginator to be used when performing a transformation. It
     * may be left empty to use Fractal's default one. This can either be a
     * string or a League\Fractal\Paginator\PaginatorInterface subclass.*/
    'default_paginator' => '',

    /*
     * League\Fractal\Serializer\JsonApiSerializer will use this value to
     * as a prefix for generated links. Set to `null` to disable this.
     */
    'base_url' => null,

    /*
     * If you wish to override or extend the default Spatie\Fractal\Fractal
     * instance provide the name of the class you want to use.
     */
    'fractal_class' => Spatie\Fractal\Fractal::class,

    'auto_includes' => [

        /*
         * If enabled Fractal will automatically add the includes who's
         * names are present in the `include` request parameter.
         */
        'enabled' => true,

        /*
         * The name of key in the request to where we should look for the includes to include.
         */
        'request_key' => 'include',
    ],

    'auto_excludes' => [

        /*
         * If enabled Fractal will automatically add the excludes who's
         * names are present in the `include` request parameter.
         */
        'enabled' => true,

        /*
         * The name of key in the request to where we should look for the excludes to exclude.
         */
        'request_key' => 'exclude',
    ],

Usage

Refer to the documentation of spatie/fractalistic to learn all the methods this package provides., (*16)

In all code examples you may use fractal() instead of Fractal::create()., (*17)

Send a response with transformed data

To return a response with json data you can do this in a Laravel app., (*18)

$books = fractal($books, new BookTransformer())->toArray();

return response()->json($books);

The respond() method on the Fractal class can make this process a bit more streamlined., (*19)

return fractal($books, new BookTransformer())->respond();

You can pass a response code as the first parameter and optionally some headers as the second, (*20)

return fractal($books, new BookTransformer())->respond(403, [
    'a-header' => 'a value',
    'another-header' => 'another value',
]);

You can pass json encoding options as the third parameter:, (*21)

return fractal($books, new BookTransformer())->respond(200, [], JSON_PRETTY_PRINT);

You can also set the status code and the headers using a callback:, (*22)

use Illuminate\Http\JsonResponse;

return fractal($books, new BookTransformer())->respond(function(JsonResponse $response) {
    $response
        ->setStatusCode(403)
        ->header('a-header', 'a value')
        ->withHeaders([
            'another-header' => 'another value',
            'yet-another-header' => 'yet another value',
        ]);
});

You can add methods to the Fractal class using Laravel's Macroable trait. Imagine you want to add some stats to the metadata of your request, you can do so without cluttering your code:, (*23)

use Spatie\Fractal\Fractal;

Fractal::macro('stats', function ($stats) {
    // transform the passed stats as necessary here
    return $this->addMeta(['stats' => $stats]);
});

fractal($books, new BookTransformer())->stats(['runtime' => 100])->respond();

Quickly creating a transformer

You can run the make:transformer command to quickly generate a dummy transformer. By default it will be stored in the app\Transformers directory., (*24)

Upgrading

From v4 to v5

Rename your config file from laravel-fractal to fractal, (*25)

From v2 to v3

v3 was introduced to swap out the league/fractal with spatie/fractalistic. Support for Lumen was dropped. You should be able to upgrade a Laravel application from v2 to v3 without any code changes., (*26)

From v1 to v2

In most cases you can just upgrade to v2 with making none or only minor changes to your code:, (*27)

  • resourceName has been renamed to withResourceName.

The main reason why v2 of this package was tagged is because v0.14 of the underlying Fractal by the League contains breaking change. If you use the League\Fractal\Serializer\JsonApiSerializer in v2 the links key will contain self, first, next and last., (*28)

Changelog

Please see CHANGELOG for more information what has changed recently., (*29)

Testing

bash $ composer test, (*30)

Contributing

Please see CONTRIBUTING for details., (*31)

Security

If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker., (*32)

Credits

License

The MIT License (MIT). Please see License File for more information., (*33)

The Versions

19/07 2018

dev-master

9999999-dev https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

19/07 2018

5.4.0

5.4.0.0 https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

13/06 2018

5.3.2

5.3.2.0 https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

08/02 2018

5.3.1

5.3.1.0 https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

28/11 2017

5.3.0

5.3.0.0 https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

15/09 2017

5.2.0

5.2.0.0 https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

08/09 2017

5.1.0

5.1.0.0 https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

30/08 2017

dev-analysis-qxwnQO

dev-analysis-qxwnQO https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

30/08 2017

dev-analysis-qybNOY

dev-analysis-qybNOY https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

30/08 2017

5.0.1

5.0.1.0 https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

30/08 2017

5.0.0

5.0.0.0 https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

24/08 2017

dev-laravel-55

dev-laravel-55 https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

22/08 2017

v4.x-dev

4.9999999.9999999.9999999-dev https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

22/08 2017

4.5.0

4.5.0.0 https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

20/08 2017

4.4.0

4.4.0.0 https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

26/07 2017

4.3.0

4.3.0.0 https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

18/07 2017

4.2.0

4.2.0.0 https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

14/06 2017

4.0.2

4.0.2.0 https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

05/05 2017

4.0.1

4.0.1.0 https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

05/05 2017

4.01

4.01.0.0 https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

26/04 2017

4.0.0

4.0.0.0 https://github.com/spatie/laravel-fractal

An easy to use Fractal integration for Laravel applications

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

22/02 2017

3.5.0

3.5.0.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel and Lumen

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

07/02 2017
04/02 2017
28/01 2017
23/01 2017
20/01 2017

3.2.1

3.2.1.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

14/01 2017

3.2.0

3.2.0.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

11/01 2017

3.1.3

3.1.3.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

11/01 2017

3.1.2

3.1.2.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

08/01 2017

3.1.1

3.1.1.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

05/01 2017

3.1.0

3.1.0.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

11/12 2016

3.0.1

3.0.1.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

09/12 2016

3.0.0

3.0.0.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

02/12 2016

2.1.0

2.1.0.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

27/09 2016

2.0.0

2.0.0.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

23/08 2016

1.9.1

1.9.1.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

30/03 2016

1.9.0

1.9.0.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

18/02 2016

1.8.0

1.8.0.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

16/12 2015

1.7.4

1.7.4.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

10/11 2015

1.7.3

1.7.3.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

09/11 2015

1.7.2

1.7.2.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

26/10 2015

1.7.1

1.7.1.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

26/10 2015

1.7.0

1.7.0.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

21/10 2015

1.6.1

1.6.1.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

20/10 2015

1.6.0

1.6.0.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

14/10 2015

1.5.0

1.5.0.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

13/10 2015

1.4.0

1.4.0.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

13/10 2015

1.3.0

1.3.0.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel api lumen transform spatie fractal laravel-fractal

11/10 2015

1.2.0

1.2.0.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-fractal

08/10 2015

1.1.0

1.1.0.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-fractal

07/10 2015

1.0.0

1.0.0.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-fractal

06/10 2015

0.0.1

0.0.1.0 https://github.com/spatie/laravel-fractal

A Fractal service provider for Laravel 5

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-fractal