Laravel transformer vs resource

16.5k Views Asked by At

I am little bit confused about the difference between Laravel eloquent resource and transformers. When i should use resource and when i should use transformers. Did some research on it from laravel documentation and medium article but its look like same purpose.

May be i have wrong concept about that. Any knowledge share from anyone will be appreciate.

2

There are 2 best solutions below

1
On BEST ANSWER

Laravel Eloquent API Resource was released on Laravel 5.5.

Before this feature exists officially, there was a package from spatie called spatie/laravel-fractal that's a wrap for league/fractal.

Both packages archives the same objective with different approach.

I prefer using the Eloquent API Resource since it's embedded on Laravel, so I reduce one (direct) dependency.

Note: The medium article uses the Fractal package, but this article is very superficial, you can find more detailed instructions on spatie/laravel-fractal, it you decide to use it.

1
On

Found this on a google search below your post:

You build out each "transformer" (which would be the equivalent of a "resource"), with the ability to include or not include whichever relationships you want solely based off the of request URL.