Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel transformer vs resource

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.

like image 680
Emtiaz Zahid Avatar asked Sep 18 '18 04:09

Emtiaz Zahid


1 Answers

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.

like image 69
Elias Soares Avatar answered Sep 16 '22 12:09

Elias Soares