Let's say I have these relationships
class Invitation
{
public function invitedPeople()
{
return $this->belongsTo('App\Person', 'personID');
}
}
class Person
{
public function apartments()
{
return $this->belongsToMany('App\Apartment', 'apartment_person', 'personID', 'apartmentID');
}
}
class Apartment
{
public function city()
{
return $this->belongsTo('App\City', 'cityID');
}
}
My question here is, how many nested levels can we have when using Laravel eager loading? I've tried this query and it's not working, can someone suggest a work around for this?
return Invitation::with(['invitedPeople', 'invitedPeople.apartments', 'invitedPeople.apartments.city'])
Change it to
return Invitation::with('invitedPeople.apartments.city')->get()
It will eager load all the related data for you. You missed the get() function. You can nest as deep as it can go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With