Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

southcoastkenny's avatar

Transform on Paginated Collection removes Pagination

Hi,

I'm trying to use transform with a paginated collection in livewire and it removes the pagination and only returns the items.

$query = Tyre::where('status', 1);
$rows = $query->paginate(25);

$results = $rows->getCollection()->transform(function($row, $key) {
	$row->money = number_format($row->retail, 2);
});

If i dump the data before the transform the collection displays as it should with paginator. After the transform it just returns a collection of the 25 items and no paginator wrapper around it.

Many Thanks.

0 likes
14 replies
MichalOravec's avatar

You forgot add return from transform.

$rows = Tyre::where('status', 1)->paginate(25);

$results = $rows->getCollection()->transform(function($row, $key) {
    $row->money = number_format($row->retail, 2);

    return $row;
});

Or

$rows = Tyre::where('status', 1)->paginate(25);

foreach ($rows as $row) {
   $row->money = number_format($row->retail, 2);
}
1 like
southcoastkenny's avatar

Hi, thanks for the answers

My bad for the typo, i do have a return $row in there, but does not work.

@snapey i do a few more transforms on the row so put that number_format in there for ease as an example.

$results = $rows->getCollection()->transform(function ($row, $key) {
    $row->money = Money::pricing($row->retail, $row->vat);
    $row->trade = Money::pricing($row->tradeprice, $row->vat);
    $row->wsale = Money::pricing($row->wsaleprice, $row->vat);
    return $row;
}

Where money is a function that calculate exchange rates, and format pricing into a an array containing multiple values.

But it just wipes the pagination from the collection and returns just the items:

Illuminate\Database\Eloquent\Collection {#1895 ▼
  #items: array:7 [▼
    0 => App\Models\Tyre {#1896 ▶}
    1 => App\Models\Tyre {#1897 ▶}
    2 => App\Models\Tyre {#1898 ▶}
    3 => App\Models\Tyre {#1899 ▶}
    4 => App\Models\Tyre {#1900 ▶}
    5 => App\Models\Tyre {#1901 ▶}
    6 => App\Models\Tyre {#1902 ▶}
  ]
}

Many Thanks

Snapey's avatar

I'm not familiar with getCollection() Where/how did you know to use that?

ps. I'd still use accessors

MichalOravec's avatar
Level 75

@southcoastkenny Don't create new variable $results, but just do it on the same variable.

$rows = Tyre::where('status', 1)->paginate(25);

$rows->getCollection()->transform(function ($row, $key) {
    $row->money = Money::pricing($row->retail, $row->vat);
    
    $row->trade = Money::pricing($row->tradeprice, $row->vat);

    $row->wsale = Money::pricing($row->wsaleprice, $row->vat);
    
    return $row;
});

return view('some-view', compact($rows));

In the view this will be still work

$rows->links();
4 likes
fahmidotexe's avatar

@MichalOravec I use laravel 8 and getCollection() is undefined, when I remove it, it works. Now, when I use transform, it still wipe out the pagination props when returning to json or inertia, help?

fahmidotexe's avatar

@MichalOravec apologize, I thought it was still relevant. I was asked this in SO and discord but haven't yet get answer and almost a half year now. So sorry 🙏.

tykus's avatar

Pagination classes have a through method which allows you to fluently iterate over the items in the Collection transforming each item. This is much cleaner than getting the Collection IMHO


$rows = Tyre::where('status', 1)->paginate(25)
    ->through(function ($row, $key) {
        $row->money = Money::pricing($row->retail, $row->vat);
        $row->trade = Money::pricing($row->tradeprice, $row->vat);
        $row->wsale = Money::pricing($row->wsaleprice, $row->vat);
        return $row;
    });

https://laravel.com/api/8.x/Illuminate/Pagination/AbstractPaginator.html#method_through

2 likes
tykus's avatar

@MichalOravec was answering the guy who was asking about this today; thanks for the history lesson

Please or to participate in this conversation.