DEV Community

Laravel API - The best way to Return the uniform Response for all type of Requests

Lakh Bawa on November 30, 2020

Hello everyone, hope you are doing well and staying safe. When designing the API, one of the most important things is to design the API in a way t...
Collapse
 
stephenjude profile image
Stephen Jude

Nice. I use Laravel Responder for this. It also returns uniform response for Laravel base response or errors like firstOrFail(), abort() etc.

You can check it out here github.com/flugger/laravel-responder

Collapse
 
bawa_geek profile image
Lakh Bawa • Edited

Hmm, that's also not bad, But I guess, to use that, We must be using Fractal Package, which is no longer required with the New Version of Laravel as we already have the functionality of Fractal in laravel core now

Collapse
 
stephenjude profile image
Stephen Jude

Can you point me to that?

Thread Thread
 
bawa_geek profile image
Lakh Bawa

I believe Laravel response has the dependency on github.com/thephpleague/fractal which is no longer required as We have Laravel Api Resources now

laravel.com/docs/8.x/eloquent-reso...

Thread Thread
 
stephenjude profile image
Stephen Jude

Oops, didn't know that's what you are referring to. Its been there for a while. Since 5.5

I fractal works for me.

Thread Thread
 
bawa_geek profile image
Lakh Bawa

yeah, thats true

Collapse
 
tarachandk profile image
Tarachand Prakash Khorwal

How can I use this in laravel 11. It's doing anything in it.

Collapse
 
syofyanzuhad profile image
Syofyan Zuhad

is this handler also can use to handling the normal (web) request

Collapse
 
bawa_geek profile image
Lakh Bawa

Updated the code with one I am using, Yes, I should handle web request normally now

Collapse
 
cauns profile image
Nguyễn Sinh Cầu

That's great, thank you very much.
One thing is that you are providing missing documents
EmptyResource,
EmptyResourceCollection

Collapse
 
tarachandk profile image
Tarachand Prakash Khorwal

Thank you so so much!!!!!