Laravel lazy eager loading with repository
<p>I started working with repositories in Laravel and came across a situation where I'm not sure if I'm handeling this the right way...</p>

<p>User repository:</p>

<pre><code>interface UserRepositoryInterface
{
    public function findByID(int $user_id);
    public function load(User $user, array $relations);
}

class UserRepository implements UserRepositoryInterface
{
    public function findByID(int $user_id)
    {
        return User::find($user_id);
    }
    public function load(User $user, array $relations)
    {
        $user-&gt;load($relations);
    }
}
</code></pre>

<p>Client basecontroller:</p>

<pre><code>protected $user_repository, $client;

class Controller extends BaseController
{
    public function __construct(Request $request, UserRepositoryInterface $user_repository)
    {
        $this-&gt;user_repository = $user_repository;
        $this-&gt;client = $this-&gt;user_repository-&gt;findByID($request-&gt;route('client_id'));
    }
}
</code></pre>

<p>Some extension of the client basecontroller:</p>

<pre><code>use App\Http\Controllers\...\Controller as ClientsController;

class SomeController extends ClientsController
{
    public function index()
    {
        $this-&gt;user_repository-&gt;load($this-&gt;client, ['addresses', 'bank_accounts', 'etc']);
        return $this-&gt;client;
    }
}
</code></pre>

<p>While the index() functions does show the client with related models, it feels like my approach is wrong, but sinds I already have the client it feels more natural to load the missing related models then to do the call below (where I fetch the user again):</p>

<pre><code>$this-&gt;client = $this-&gt;user_repository-&gt;findByIDWithRelations($user_id, ['...']);
</code></pre>

<p>Because the load() function in the repository doesn't return anything and I assign nothing in the index() function it feels bogus somehow... Can anyone confirm or deny this?</p>

<p>Update:</p>

<p>Take for example this piece of code below (does not work):</p>

<pre><code>function addToArray($array, $value)
{
    array_push($array, $value);
}

$array = ['a', 'b', 'c'];

addToArray($array, 'd');

foreach($array as $value)
{
    echo $value;
}
</code></pre>

<p>The way I approached this in Laravel feels very similar, which makes me feel it's wrong :-).</p>