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 see all of the options for an artisan command?

How to fix the MySQL related "Specified key was too long error" error in Laravel

How do you access the php://input stream?

Is multiple inheritance supported in PHP?

How to disable file hotlinking in Apache with .htaccess

How to output (echo/print) content in PHP

How to show (or log) all SQL queries executed by Laravel

How to get table column names from a database table in Eloquent?

What are generators in PHP, and how do they compare to arrays?

How to block multiple IP addresses in .htaccess