[12.x] Allow rendering paginated data where cursor parameters are not in the response #55524
+60
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If you attempt to render a CursorPaginator, but do not have the keys/aliases as properties on the underlying data, a warning is raised. On our production environments, this returns a 500.
Take for instance something like this:
I do not want to expose the integer ID nor the created at timestamps in my response, but this will produce a warning. The reason the warning is raised is because
through
transforms the items, butAbstractCursorPagination::getParametersForItem()
looks to find the cursor parameters as properties on the modified data.With this PR's changes, I can now do this:
This is particularly meaningful if I want to transform my data into
Responsable
DTOs (with something Laravel Data). While Laravel-Data does offer a paginated data collection class, it's not necessarily as easy to work with as the one that comes with the framework.If I have a Responsable object like this:
Then cursor pagination will fail because there's no
id
field in the UserResponse.Other possibilites
Change the
through
method so it does not immediately act on the collection, but instead only does it attoArray()
time. This would potentially be a breaking change for users that relied on this behavior.Additionally, the signature of
through
could be changed to:Though this is a breaking change for anyone who has extended
CursorPaginator
. We could take the pseudo-argument approach where we check for a second argument being passed in, but not change the signature.We could also create the cursor if one isn't set when the paginator is constructed. That could be done inside of setItems or directly after it. I am starting to think that this is cleaner.