staudenmeir/laravel-adjacency-list

Is Eager Loading supported?

CoderPlusFromNowhere opened this issue · 2 comments

In the docs there is no Eager Loading support using with($relation) (https://laravel.com/docs/5.2/eloquent-relationships#eager-loading), so I assume that there is no support right?

What is the best way to achieve this if there is no Eager Loading support?

$users = User::withMaxDepth($untilLevel, function () use ($request) {
            return User::find($request->user()->id)->descendants;
        });

Then I need to get all user data from another model called UserData in a BelongsTo(one-to-one) user->id <-> userData->id. I can easily loop the entire $users descendants tree and get the userData:

$users[0]->userData->id

But the problem is that it will be lazy loaded and will generate a N+1 query problem. This problem is solved using Laravel Eager Loading feature.

Don't understand what you try to achieve but all package relationships can be eagerly loaded.

You can do this I guess

$users->load('userData');

// or
User::find($request->user()->id)->descendants()->with('userData')->get();

Ouch, got it.

I tried to access with() with descendants property and not with descendants() function. That was the problem. Thanks.