How to eager load multiple levels of relationships in Eloquent?

Laravel: How to eager load multiple levels of relationships in Eloquent?

You can use the 'dot notation' to eager load multiple levels of relationships in Eloquent

  1. $posts = Posts::with("comments.author")->get();

This will load the comments relationship, and load the authors for each comment. Of course this assumes that you have set up the relationships correctly in your models

webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to eager load multiple levels of relationships in Eloquent?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the Laravel language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to eager load multiple levels of relationships in Eloquent?

How to return an item in Eloquent, or throw a ModelNotFoundException exception if it isn't found?

How to check if an object or variable is an array, in JS?

A list of operators on Eloquent's where() method

What is the max size, by default, of GET variables?

How to check if a (blade) view file exists

How to use subdomains as parameters in your routes (or in your web.php file)

How to set up an alias to map one directory to another destination

How to hide certain file types from directory listings

How could you include a custom function for every single time PHP runs?