81

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?

Jan Żankowski
  • 8,690
  • 7
  • 38
  • 52
DrivingInsanee
  • 1,631
  • 2
  • 13
  • 22

5 Answers5

152

You can do

 $books = App\Book::with('author.contacts','author.publishers')->get();
oseintow
  • 7,221
  • 3
  • 26
  • 31
  • 8
    just to add, it will fetch the corresponding author data as well ofcourse. – StealthTrails Apr 10 '17 at 18:56
  • Follow-up: I have a much more complex model and want to return a single collection, but like the original poster, wish I could use multiple nested relationships at a lower child level like `$event = event::with(['streams.experiences.selectors.['digitalprops.frames','filters']','streams.datacaptures'])->find($eventcode);` – Todd Jul 12 '17 at 01:31
  • @Todd How do you execute this code in Laravel? `$event = event::with(['streams.experiences.selectors.['digitalprops.frames','filters']','streams.datacaptures'])->find($eventcode);`. It shouldn't work...! – tisuchi Jun 25 '18 at 17:46
  • Right @tisuchi -- I said I *WISH* I could... sorry for the confusion. – Todd Jun 25 '18 at 18:17
  • does author is table name or what? – msbomrel Sep 14 '19 at 05:04
  • @msbomrel, author is a relationship method in the Book class – Elisha Senoo Sep 30 '19 at 17:17
48

Laravel documentation on eager loading recommends listing the relationships in an array as follows:

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

You can have as many relationships as desired. You can also specify which columns should be included for a relationship like this:

//only id, name and email will be returned for author
//id must always be included
$books = App\Book::with(['author: id, name, email', 'author.contacts', 'author.publishers'])->get();

You may also add constrains as follows:

$books = App\Book::with(['author: id, name, email', 'author.contacts' => function ($query) {
                                          $query->where('address', 'like', '%city%');
                                     }, 'author.publishers'])->get();
Elisha Senoo
  • 3,489
  • 2
  • 22
  • 29
11

So, now you can try

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

From Laravel 9, the neatest way is the nested array :

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

Reference

Waseem Alhabash
  • 498
  • 4
  • 12
0

When eager load nested relationships and we want to select just some columns and not all using relationship:id,name, always include the foreign key to the nested models, else they won't load at all.

Fort example, we have orders that have identities that have addresses.

This will not load the address:

User::orders()
    ->with('identity:id,name', 'identity.address:id,street')

This will load the address because we have supplied the address_id foreign key:

User::orders()
    ->with('identity:id,address_id,name', 'identity.address:id,street')
Christos Lytras
  • 36,310
  • 4
  • 80
  • 113