@@ -140,7 +140,7 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
140
140
$ table = '' ;
141
141
$ deletedAt = false ;
142
142
$ lastQuery = $ this ->query ;
143
- foreach (explode ('. ' , $ relation ) as $ eachRelation ) {
143
+ foreach (explode ('. ' , $ relation ) as $ index => $ eachRelation ) {
144
144
$ model = $ lastQuery ->getRelation ($ eachRelation );
145
145
switch (true ) {
146
146
case $ model instanceof BelongsToMany:
@@ -169,19 +169,24 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
169
169
170
170
case $ model instanceof BelongsTo:
171
171
$ table = $ model ->getRelated ()->getTable ();
172
+ $ alias = "alias_ {$ index }_ {$ table }" ;
173
+ $ tableAs = "{$ table } AS {$ alias }" ;
172
174
$ foreign = $ model ->getQualifiedForeignKeyName ();
173
- $ other = $ model ->getQualifiedOwnerKeyName () ;
175
+ $ owner = "{ $ alias } . { $ model ->getOwnerKeyName ()}" ;
174
176
$ deletedAt = $ this ->checkSoftDeletesOnModel ($ model ->getRelated ());
177
+
175
178
break ;
176
179
177
180
default :
178
181
throw new Exception ('Relation ' . get_class ($ model ) . ' is not yet supported. ' );
179
182
}
180
- $ this ->performJoin ($ table , $ foreign , $ other , $ deletedAt );
183
+ $ this ->performJoin ($ tableAs ?? $ table , $ foreign , $ owner ?? $ other , $ deletedAt );
181
184
$ lastQuery = $ model ->getQuery ();
182
185
}
183
186
184
- return $ table . '. ' . $ relationColumn ;
187
+ $ table = $ alias ?? $ table ;
188
+
189
+ return "{$ table }. {$ relationColumn }" ;
185
190
}
186
191
187
192
protected function checkSoftDeletesOnModel ($ model )
0 commit comments