Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

salebab's avatar

Laravel and Fractal

Hi all,

As you probably heard in casts, @JeffreyWay mentioned Fractal, a library for APIs. The implementation is not so hard, but I've tried to abstract more, so I've created a library called larasponse.

If you are interested in, please take a look and I'll be very pleased to hear some comments and reviews :)

0 likes
29 replies
jgravois's avatar

I am very interested in Fractal because I have an API that is getting out of control. I am using the Transformers @JeffreyWay taught in the Incremental API series and though I have researched for the last 2 days, I am still clueless as how to switch to Fractal.

I am interested in using larasponse but am not good enough to do so with the instructions on the readme page. Can you point me to a resource or resources to make this tranformation (I guess your "the implementation is not so hard" comment wasn't meant for me).

salebab's avatar

Hi @jgravois thanks for the comment.

You can read more about transformers here - http://fractal.thephpleague.com/transformers/ scroll down for "Classes for Transformers". Also, scroll more and see how to nest and transform related objects.

I'll try to update a library with more examples, but it's crucial for you to know how Fractal works.

jgravois's avatar

Hello, again @salebab

I was wondering if you have a "barebones" project with your Fractal package installed and in use that I could tear to pieces and then experiment with to learn. I am having great troubles setting this up.

mabasic's avatar

I am also having problems using Fractal. I want to create a service provider for fractal, but I don't know how

mabasic's avatar

Yeah, but I am having problems implementing in Incremental API series.

salebab's avatar

You do not need to create a service provider, you can use larasponse or mentioned laravel-fractal. Only you need is to register a service provider in your app.php.

Btw, laravel-fractal lib seems more complete, there is a facade, and probably more options, but personally, I don't like the architecture.

On the other hand, with larasponse you will write less and more readable code, especially when you need to output paginated collection. Also, there is a default transformer that can output object as it is, include key is configurable on runtime, etc.

If you have a trouble to setup, please be more specific, it should be really straightforward, I've used on few projects and it works like a charm.

mabasic's avatar

@salebab I could not resist, I had to try it and it works. But how do you use serializers with this package?

I have this:

$manager = new Manager();
$manager->setSerializer(new DataArraySerializer());
$resource = new Collection($arrangements, new ArrangementTransformer());
return $manager->createData($resource)->toArray();

Is there a way I can optimize this.

Also if I use the code above I have no use for your package because this line

return $this->fractal->collection($arrangements, new ArrangementTransformer());

is of not much use to me if I want to wrap item and collection in data.


Also I am getting output for collection:

{
    "data": [
        {
            "active": false,
            "featured": false,
            "order": 1,
            "slug": "1412085610-quibusdam"
        },
        {
            "active": false,
            "featured": true,
            "order": 2,
            "slug": "1412085611-non"
        }
    ]
}

And for single item:

{
    "data": {
        "active": false,
        "featured": false,
        "order": 1,
        "slug": "1412085610-quibusdam"
    }
}

But in Fractal documentation this is the correct output:

// Item
[
    'data' => [
        'foo' => 'bar'
    ],
];

// Collection
[
    'data' => [
        [
            'foo' => 'bar'
        ]
    ],
];

Why is there a difference in brackets?

Can you please post a full example on how you do that.

salebab's avatar

@mabasic I choose to use ArraySerializer, but if you want to use other, just bind it:

App::bind('League\Fractal\Serializer\SerializerAbstract', 'League\Fractal\Serializer\DataArraySerializer');

So, you will define a serializer and then it will be used globally for all responses.

For the second example, your application outputs JSON object, but Fractal itself returns php array. You can dd($this->fractal->collection($arrangements, new ArrangementTransformer())); in your controller and you will see array.

mabasic's avatar

@salebab I will try the code above soon and post back soon. It looks very promising.

mabasic's avatar

Perfect, it works like a charm. Thank you for your explanation and guidance.

I have created a service provider for DataArraySerializer:

<?php namespace Acme\Providers;

use Illuminate\Support\ServiceProvider;

class FractalSerializerServiceProvider extends ServiceProvider {

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        /**
         * See: http://fractal.thephpleague.com/serializers/
         */
        $this->app->bind(
            'League\Fractal\Serializer\SerializerAbstract',
            'League\Fractal\Serializer\DataArraySerializer'
        );
    }

}

And now in my controller I have:

protected $fractal;

    public function __construct(Larasponse $fractal)
    {
        $this->fractal = $fractal;
    }

 public function index()
 {
        $arrangements = Arrangement::all();

        return $this->fractal->collection($arrangements, new ArrangementTransformer());
 }

Nice and clean. Thank you very much.

1 like
isaackearl's avatar

Hello! I stumbled across larasponse today and it looks beautiful. I'm hoping to include it in a laravel 5 api I'm working on, but it looks like it has been updated in 6 months. I'm nervous about including it in my project as it says that the laravel 5 version is still in development and it looks like it hasn't been touched in awhile. Any news on whether it will continue to be supported or if a stable branch will be released for laravel 5?

davorminchorov's avatar

No idea about the update but you can use Fractal itself, which is not connected to any framework. The API of the package is almost the same.

davorminchorov's avatar

@pwm Which one? Larasponse? It's a Fractal wrapper for Laravel specifically.

If you mean Fractal, well check the docs and compare the features between the 2 libraries.

pwm's avatar

Yes, sorry, I meant Fractal. I just stumbled upon it today, and don't quite understand what problems are they solving.

I have used JMS\Serializer for a large Laravel (4.2) project with Doctrine. With Doctrine+JMS, type annotations on exposed entity attributes are read automatically. In my opinion this elegantly solves the Resource+Transformer part of Fractal, while JMS offers json, xml and yaml serialization.

Edit: Just reread my comment, apologies if it came through as negative. I was just wondering if I'd missed something re Fractal.

Please or to participate in this conversation.