vendredi 14 décembre 2018

Laravel eager loading deep relationships

I've hit a bit of a snag with Laravel and eager loading relationships. I have three models AssetFolder, Asset & DownloadLog

  • AssetFolder HasMany Asset via assets
  • Asset HasMany DownloadLog via downloads

I'm currently querying for a particular asset folder, and in doing so I'm eager loading the assets.

$folder = AssetFolder::where('name', 'Example')
    ->with([
        'assets' => function (HasMany $query) {
            $query->orderBy('name', 'asc');
        }
    ])->first();

This part works great. But for the purposes of this query, I'd also like to eager load each Asset associated DownloadLog records. I've tried solution #1 -

$folder = AssetFolder::where('name', 'Corporate Assets')
    ->with([
        'assets' => function (HasMany $query) {
            $query->orderBy('name', 'asc');
        },
        'assets.downloads'
    ])->first();

That doesn't work - but I can kind of understand that. So then I tried solution #2 -

$folder = AssetFolder::where('name', 'Corporate Assets')
    ->with([
        'assets' => function (HasMany $query) {
            $query
                ->with('downloads')
                ->orderBy('name', 'asc');
        }
    ])->first();

That doesn't work either. Now I would have expected that to work but it didn't. So to test, I went into my AssetFolder model and updated my HasMany relationship method to the following -

public function assets()
{
    return $this
        ->hasMany(Asset::class)
        ->with('downloads');
}

And that works! That's great, but the problem is that 90% of the time, I don't want to pull the download logs along with the assets I'm loading. That's why I thought solution #2 would work because I thought it simply altered the HasMany query.

Am I doing something wrong here?

Chris.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire