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.