Laravel Eloquent: eager loading of multiple nested relationships

DrivingInsanee picture DrivingInsanee · Feb 18, 2016 · Viewed 35.5k times · Source

What laravel says:

$books = App\Book::with('author.contacts')->get();

What I need is something like this

$books = App\Book::with('author[contacts,publishers]')->get();

where we eager load multiple relationships within a relationship.

Is this possible?

Answer

oseintow picture oseintow · Feb 18, 2016

You can do

 $books = App\Book::with('author.contacts','author.publishers')->get();