How to use transform in paginated collection in laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How to Use Transform in a Paginated Collection in Laravel
As a senior developer working with Laravel, you frequently encounter situations where you need to manipulate data retrieved from a database, especially when that data is wrapped within a paginated result. A common stumbling block arises when trying to apply collection methods like transform() or map() directly to a LengthAwarePaginator object, as the internal structure of pagination often hides the simple collection you are actually interested in.
This guide addresses your struggle: why applying transformations to a paginated collection results in the loss of vital pagination metadata, and how to achieve your desired outcome while retaining all necessary information.
The Problem: Why transform() Fails on Paginators
You correctly noted that methods like getCollection() are not directly available on the LengthAwarePaginator. When you call $query->paginate(...), Laravel returns an instance of LengthAwarePaginator. While this object contains a collection, it is fundamentally a paginator wrapper designed to handle navigation details (total counts, current page, next/previous links).
When you apply methods like transform() or map() directly to the paginator object, you are transforming the paginator itself, not just the underlying set of items. This operation often discards the pagination context, resulting in a simple collection instead of a paginated result, as demonstrated by your observation: "there is no pagination details in my result."
The confusion stems from trying to treat the paginator like a standard Collection when it is actually a specialized object managing both data and metadata.
Solution 1: Transforming the Items Only (When Pagination Details are Unnecessary)
If your goal is purely to modify the data records themselves and you do not need the pagination links or total counts in the final output, you should operate on the underlying collection before pagination is applied, or use methods that target the items directly.
However, if you must start from a paginated result, you can access the underlying collection via the items() method provided by the paginator:
$query = User::filter($request->all()->with('applications'));
// 1. Paginate the query first
$paginator = $query->paginate(config('app.defaults.pageSize'));
// 2. Get the collection of items *before* transformation
$items = $paginator->items();
// 3. Apply transformation to the items
$transformedItems = collect($items)->map(function ($user) {
$user['picture'] = $user['avatar'];
return $user;
});
// Note: This result ($transformedItems) is just a simple collection, not a paginator.
This approach successfully transforms your data, but you lose the pagination links when passing $transformedItems back to your view or API response.
Solution 2: Transforming While Retaining Pagination (The Correct Approach)
If you need both the transformed items and the pagination metadata (like total items and current page), you must transform the underlying collection and then re-wrap it into a new paginator instance. This ensures that all necessary context is preserved, adhering to good architectural practices seen within the Laravel framework documentation on data handling.
Here is the robust way to achieve this:
$query = User::filter($request->all()->with('applications'));
// 1. Paginate the query
$paginator = $query->paginate(config('app.defaults.pageSize'));
// 2. Transform the items within the paginator
$transformedItems = $paginator->items()->map(function ($user) {
$user['picture'] = $user['avatar'];
return $user;
});
// 3. Create a new paginator using the transformed items and the original pagination metadata
$transformedPaginator = new LengthAwarePaginator(
$transformedItems,
$paginator->total(), // Use the total count from the original paginator
$paginator->perPage(),
$paginator->currentPage(),
$paginator->links() // Keep the pagination links intact
);
// Now, $transformedPaginator contains your modified data AND full pagination details.
This pattern is crucial for maintaining data integrity when dealing with complex result sets in Laravel applications. For more advanced data manipulation or resource handling, always refer to best practices documented on the official Laravel documentation.
Conclusion
The key takeaway is that a LengthAwarePaginator is a specialized container. To perform transformations while retaining pagination context, you must explicitly extract the underlying collection using $paginator->items(), apply your mapping logic to that collection, and then reconstruct a new LengthAwarePaginator using the original metadata. This ensures that your application remains fully functional, providing both transformed data and correct navigation links for the user.