We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Ever wanted to transform the paginator results to return a subset of fields and not all? Better use the through()
function instead of the map()
function.
Instead of doing this (which replaces the paginator with a new collection):
$users = User::paginate(10)->map(fn ($user) => [
'id' => $user->id,
'name' => $user->name;
]);
You should be using the through method instead (which keeps the result a paginator):
$users = User::paginate(10)->through(fn ($user) => [
'id' => $user->id,
'name' => $user->name;
]);
Thanks to @bhaidar for the tip.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.