18

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.

Nazmul Hasan
  • 10,130
  • 7
  • 50
  • 73
Emtiaz Zahid
  • 2,645
  • 2
  • 19
  • 35

2 Answers2

30

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.

Elias Soares
  • 9,884
  • 4
  • 29
  • 59
-6

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.

David Valenza
  • 422
  • 4
  • 5