Let's say I have users which have many computers which belong to a certain computer_type (users->computers->computer_type).
I know that I can load both users and their computers with: User::with("Computer")
I would like to load all three. How can I do it in Laravel?
Eager loading is super simple using Laravel and basically prevents you from encountering the N+1 problem with your data. This problem is caused by making N+1 queries to the database, where N is the number of items being fetched from the database.
Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager loading is achieved by the use of the Include method. It means that requesting related data be returned along with query results from the database.
} Dynamic properties are "lazy loading", meaning they will only load their relationship data when you actually access them. Because of this, developers often use eager loading to pre-load relationships they know will be accessed after loading the model.
To retrieve relationships that far down, you must say the child, then the child with it's child, and so on...
User::with(array('computer', 'computer.type'))->find(1);
User has_one Computer has_one type in this particular scenario.
Try
Appointment::with('kid.parent')->get()
if each kid has a appointment and each kid has a parent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With