P
P
Pavel Makarov2015-01-28 23:06:22
Laravel
Pavel Makarov, 2015-01-28 23:06:22

Should Laravel Repository return objects or arrays?

Hello!
When working with Laravel and trying to use repositories, the following dilemma arose.
Most online resources and tutorials suggest the following repositories:

<?php
class UserRepository implements UserRepositoryInterface
{
    public function all()
    {
        return User::all();
    }
}

However, the framework creator's book "From Apprentice To Artisan" (as well as some other sources) has the following option:
<?php
class UserRepository implements UserRepositoryInterface
{
    public function all()
    {
        return User::all()->toArray();
    }
}

In the first case, the repository returns Eloquent objects (more precisely, a wrapper over them), in the second, they are all converted to arrays.
FormBuilder works correctly with both models and arrays.
In both cases, we can output custom attributes.
In the first case, there is a binding to the storage implementation itself (which, in fact, breaks the whole concept of repositories), in the second - no.
In the first case, it’s an outright lie that we just take it and can use another database. We can, but during development we will have to be careful not to call any methods, and in case of a database change, fill something like stdClass.
In the first case, we always get an object with properties and methods (which are lost if we no longer want to use Eloquent), in the second, just an unknown array (you can fix this with wrappers).
In the first case, we can get requests during rendering of the view by the view string (which can then be difficult to find)
<?php
$user->role->name;

In the second one, this will not happen (which, in general, is good) unless we explicitly do something like this in the repository, i.e. all requests are exclusively in repositories:
<?php
User::with('role')->get()->toArray();
$user['role']['name']

In the first case, we can easily use the following code:
<?php
$repo->newInstance(); // вернёт новый созданный объект Eloquent
echo $repo->id;

In the second, when trying to do something similar, we will get an error.
<?php
$repo->newInstance(); // новый созданный объект Eloquent будет преобразован к массиву
echo $repo['id']; // => error: undefined index

Eloquent doesn't care at all whether these properties are present in the database or not, it simply stores a list of passed values ​​(via new, fill, etc.) and converts them to an array. Any request for a property for which there is no value will return null, even if there is no such field in the database. This discovery was a little shocking.
This can be solved with the following code:
<?php
array_get($repo, 'id');

Or, again, use some wrappers, and not return an array.
Actually a question!
What do you use and what do you think is worth using? The first way, in fact, is not the repository that many authors of Laravel books are trying to tell us about, although it is more convenient than arrays.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Brezhnev, 2015-01-30
@vanchelo

I always use objects because I love the possibilities they provide.
Accessing a model property is much prettier than accessing an array key + does not require checking for the existence of a property, as is the case with an array key.
I prefer the first option of working with objects.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question