Miftah Afina
Miftah Afina

Reputation: 295

Eager loading inside eager loading in Laravel Eloquent

How to use eager loading inside eager loading? I tried make query like this: Here is my query in controller:

$user_all = User::with('universitas_has_detil')
->whereHas('universitas_has_detil', function ($query) {
    $query->where('jenjang_universitas_id_jenjang', 2)
})->get();

And here is my view:

@foreach($user->universitas_has_detil as $detil)
{{ $detil->jenjang_universitas->nama_jenjang }},
@endforeach

But the result is like this, and not efficient. enter image description here

Upvotes: 2

Views: 395

Answers (1)

Miftah Afina
Miftah Afina

Reputation: 295

I found the answer by my self, I should using Nested Eager Loading that describes in Laravel documentation

Upvotes: 3

Related Questions