Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel : How do I chain eager loading between four or more tables?

This is my current relationships

I -> belongsTo -> S //in model I
S -> belongsTo -> C //in model S
C -> belongsTo -> T //in model C

T -> hasMany -> C //in model T
C -> hasMany -> S //in model C
S -> hasMany -> I //in model S

Right now, I am able to eager load until C from I but how do I eager load until T also from I? I have tried several ways such as I::with(['S.c'=>function($query){ $query->with('C.t') }])

but they all spit out errors.

like image 381
John Evans Solachuk Avatar asked May 04 '15 11:05

John Evans Solachuk


1 Answers

You can eager load the descendant tables by using dot notation:

I::with(['S', 'S.C', 'S.C.T'])->get();
like image 199
undefined Avatar answered Nov 10 '22 13:11

undefined