10

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

To me, it looks like the same thing?

Oliver Bayes-Shelton
  • 6,135
  • 11
  • 52
  • 88
  • 5
    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. – Mihir Bhende Feb 18 '19 at 13:55
  • 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. Thanks – Oliver Bayes-Shelton Feb 18 '19 at 14:01
  • 2
    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 :) – Mihir Bhende Feb 18 '19 at 14:04
  • 1
    Awesome thanks :) Can you write that as an aswer and I will make as answered. – Oliver Bayes-Shelton Feb 18 '19 at 14:12

2 Answers2

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

Mihir Bhende
  • 8,677
  • 1
  • 30
  • 37
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 !!)

Mahdi Youseftabar
  • 2,273
  • 1
  • 22
  • 29