You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently using Laravel 7.12.0 there's a few ways of returning a JSON response within a controller, from paginated data coming from a LengthAwarePaginator, but they all seem to return different JSON responses/content.
While 2 it's kinda understandable - you can override the behaviour by customizing the toArray method in the collection, 1 and 3 look very similar: both ways detect a paginator properly and add the same meta-data on top of it, but in a slightly different structure, and both ways "know" they are getting returned within a JSON response, so it really feels like they should be the same.
What do you think?
Happy to submit an issue with this, or a reproduction case/repo if you want me to :)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Currently using Laravel 7.12.0 there's a few ways of returning a JSON response within a controller, from paginated data coming from a
LengthAwarePaginator
, but they all seem to return different JSON responses/content.Assuming I'm paginating
User
models:response
helper methodReturns something along the lines of:
make:resource UserCollection
) with the response helper method:Returns exclusively the data part a.k.a. just the collection part of the paginator:
response()
call):Returns data similar to the first example, but wrapped a bit further:
While 2 it's kinda understandable - you can override the behaviour by customizing the
toArray
method in the collection, 1 and 3 look very similar: both ways detect a paginator properly and add the same meta-data on top of it, but in a slightly different structure, and both ways "know" they are getting returned within a JSON response, so it really feels like they should be the same.What do you think?
Happy to submit an issue with this, or a reproduction case/repo if you want me to :)
Beta Was this translation helpful? Give feedback.
All reactions