Category: Laravel, PHP

Date: July 11, 2020Version: Laravel 7.x Using the Laravel Eloquent Builder, thetoArray and toJson were not structuring the object in a way I preferred.

Other approaches could involve creating utility classes that restructure things for you, like Utility::toJson($myCollection) but that doesn’t flow naturally into the Eloquent Builder.

That would look like this: This gives you more flexibility over what happens within the paginate function itself, but isn’t necessarily the best solution if you don’t need that capability.

By overriding this function, you are also taking control over the $page, $perPage, and $results variables which may be subject to change in future versions of Laravel.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners