phplaraveleloquentcastingrepository-pattern

Casting Eloquent\Collection (Laravel) to stdClass array using Repository pattern


I'm trying to implement the Repository pattern in a Laravel 5 app by following this article. In it, the repository implementation converts the object for the specific data source (in this case Eloquent) to an stdClass so that the app uses a standard format and doesn't care about the data source.

To convert a single Eloquent object they do this:

/**
* Converting the Eloquent object to a standard format
* 
* @param mixed $pokemon
* @return stdClass
*/
protected function convertFormat($pokemon)
{
    if ($pokemon == null)
    {
        return null;
    }

    $object = new stdClass();
    $object->id = $pokemon->id;
    $object->name = $pokemon->name;

    return $object;
}

Or, as someone in the comments pointed out, this could also work:

protected function convertFormat($pokemon)
{
    return $pokemon ? (object) $pokemon->toArray() : null;
}

But then, what happens when I want to cast an entire collection of Eloquent objects to an array of ** stdClass **? Do I have to loop through the collection and cast each element separately? I feel this would be a big hit on performance, having to loop and cast every element every time I need a collection of something and it also feels dirty.

Laravel provides Eloquent\Collection::toArray() which turns the entire collection to an array of arrays. I suppose this is better, but still not stdClass

The benefits of using a generic object would be that I could do this in my code

echo $repo->getUser()->name;

Instead of having to do this:

echo $repo->getUser()['name'];

Solution

  • Yes, you would need to loop through the collection and cast every object. You can save a few lines of code by using array_map.