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

What is camel case?

Point all requests to one PHP file

How to Extract Query String (from a URL) Into an Associative Array in PHP

How to split a string into an array, in JS

Why should you update APP_URL from from http://localhost?

How to select all rows for a certain month (or day, year or time), using Eloquent

How to do comments in Blade files?

How to review blog posts (written by other users) before they get published on your WordPress blog?

What is the difference between reference and primitive data types types in Javascript?

How to merge/concat two arrays together in JS?