beyondcode/laravel-query-detector

Eager loaded pivot relationships still show alert?

godbout opened this issue · 6 comments

Maybe I'm doing something wrong, not sure, but I don't think I've got a N+1 query issue in my case. I still have the alert though.

I have a Section class. We can get the students and/or teachers of a section through properties. Those properties are eager loaded through the $with public variable. Still, the alert shows up.

public function students()
    {
        return $this->belongsToMany(User::class)->wherePivot('type', 'STUDENT');
    }

    public function teachers()
    {
        return $this->belongsToMany(User::class)->wherePivot('type', 'TEACHER');
    }

It happens when I show a section. I get the Section object through the controller method, and just send it back to the view with all eager loaded.

Any idea?

jtrod commented

Hi. Did you find any solution? I'm facing the same issue. I have eager loaded relations on the $with property.. but the alert still shows.

@mpociot Is there a way for not triggering the alert when using $with?

can't remember. probably gave up.

similar problems. can it be used for multy relation? like users has relation to roles and anyrelation.
in my controllers :

$usersBinding = User::with(['roles', 'anyrelation' => function($teacher){
            $teacher->with(['classAcceses' => function($classAccess){
                $classAccess->with('class')->get();
            }])->get();
        }])->whereIn('id',$roleIds);

still geting alert in the browser. Any Solutions?

Anyway... https://twitter.com/taylorotwell/status/1395087054254526474?s=20

where should i place that code?

Anyway... https://twitter.com/taylorotwell/status/1395087054254526474?s=20

where should i place that code?

Hej @perffy, this is documented.