Reputation: 2219
In Laravel 5.5+, we can use API resource to format our API response in a way we want. I want to create API resource in Laravel 5.4 project which I can't upgrade to 5.5. Is there any way to use API resource in Laravel 5.4?
If not, what is the best way to format response and how to properly format responses while creating an API with Laravel? What are the tips and best practices to create API in Laravel version 5.4, 5.3, 5.2 etc?
Upvotes: 0
Views: 1075
Reputation: 58322
I would highly recommend using Fractal by the League of PHP: http://fractal.thephpleague.com/
The concept is the same (typically a "resource" is called a transformer). They tackle both single items and collections and act as a simplification layer between the actual data and what you want presented through your endpoint.
I have been using this in 5.0 through to 5.4 without issue.
Upvotes: 3