Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

You can use something like this

School::with('teachers','teachers.pupils')->get();

Just adust the names of your relationships

Last updated 2 years ago.
0

pixelpeter said:

You can use something like this

School::with('teachers','teachers.pupils')->get();

Just adust the names of your relationships

That will eager load pupils but not combine them.

I think the only way for now could be something like this:

Pupil::whereIn('teacher_id', function($query) use ($school) {
    $query->select('id')->from('teachers')->where('school_id', $school->id);
});
Last updated 2 years ago.
0

cheers popolla your suggestion worked!

maybe pushing it here but is there any way to add in another level?!

something like area->school->teachers->pupil ?

thanks again.

Last updated 2 years ago.
0

by the way for anyone looking in I had to add ->get() onto the end of popolla's query otherwise there was a recursion error

Last updated 2 years ago.
0

lainga9 said:

maybe pushing it here but is there any way to add in another level?!

something like area->school->teachers->pupil ?

Try this one:

$pupils = Pupil::whereIn('teacher_id', function($query) use ($area) {
    $query->select('teachers.id')->from('teachers')
        ->join('schools', 'teachers.school_id', '=', 'schools.id')
        ->where('schools.area_id', '=', $area->id);
})->get();
Last updated 2 years ago.
0

works perfectly - thanks a lot!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

lainga9 lainga9 Joined 12 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.