Eloquent: API Resources vs Fractal
Asked Answered
I

2

11

Quick question, what is the difference between Eloquent: API Resources and Fractal?

To me, it looks like the same thing?

Impetigo answered 18/2, 2019 at 13:49 Comment(4)
Both are used to transform API json responses to standardise the response structure. However, API resources is inbuilt in laravel and its very easy to use. Fractal was the preferred way to go when API resources were not inbuild in laravel. Fractal has some methods which make it little extensive as compared to API resources. But if you consider the core functionaity, both are same with different syntactical sugers.Impermanent
I thought that was the case, I had been using Fractal for the last couple of years and always found it fiddly to get set up with Laravel but once it worked it was great but not I might as well just use the inbuilt system. ThanksImpetigo
Exactly, I have had fractal in my older projects, API resources just eliminated the need to do initial fractal setup. And the nomenclature is very easy in API resources to start with :)Impermanent
Awesome thanks :) Can you write that as an aswer and I will make as answered.Impetigo
I
17

Both are used to transform API json responses to standardise the response structure.

However, API resources is inbuilt in Laravel and it's very easy to use. Fractal was the preferred way to go when API resources were not in-build in Laravel. Fractal has some methods which make it little extensive as compared to API resources.

But if you consider the core functionality, both are same with different syntactical sugar.

Most of the things which were in fractal, you can do natively in Laravel now. Plus API resources eliminate the need of any extra installation and setup. The nomenclature is very easy in API resources to start with

Impermanent answered 18/2, 2019 at 14:15 Comment(0)
B
3

both of them are created for one job but their solutions are different in many ways.

Relationships:

in fractal you can easily add related models to the response. also, you can control when the related models should be presented in the response . (default include vs Available include)

for example your client can use ?include=rate to get the rate model from an article when needed! consider that, the fractal will eager load your relationships when you forgot to load it.

in API Resources you have no control over relationships and you should decide to have relationship or not in the first place. otherwise, if you forgot to eager load data for it, it will cost you too many queries to load related model (1+n problem).

Serializer

in basic usage of api resource you have no control on how data will map to final response.

for example if you want jsonnapi specification for your responses, you should manage all of the works by yourself. but in fractal you have it in the first place.

as a conclusion i recommend you to use fractal in this case. (or use dingo package for api but consider complexity of dingo !!)

Bolide answered 7/7, 2020 at 20:32 Comment(1)
This answer is no longer relevant as of (at least) Laravel 11, as you can read in the docs: laravel.com/docs/11.x/eloquent-resources. There's built-in facilities now allowing to manage relationships (including conditional loading), as well as custom data wrapping.Boyette

© 2022 - 2024 — McMap. All rights reserved.