@@ -115,14 +115,19 @@ public function joinRelationship(): Closure
115115 return $ this ;
116116 }
117117
118+ $ relationCallback = $ callback ;
119+ if ($ callback && is_array ($ callback ) && isset ($ callback [$ relationName ]) && is_array ($ callback [$ relationName ])) {
120+ $ relationCallback = $ callback [$ relationName ];
121+ }
122+
118123 $ relation = $ this ->getModel ()->{$ relationName }();
119124 $ relationQuery = $ relation ->getQuery ();
120125 $ alias = $ joinHelper ->getAliasName (
121126 $ useAlias ,
122127 $ relation ,
123128 $ relationName ,
124129 $ relationQuery ->getModel ()->getTable (),
125- $ callback
130+ $ relationCallback
126131 );
127132
128133 if ($ relation instanceof BelongsToMany && !is_array ($ alias )) {
@@ -131,12 +136,13 @@ public function joinRelationship(): Closure
131136 $ relation ,
132137 $ relationName ,
133138 $ relation ->getTable (),
134- $ callback
139+ $ relationCallback
135140 );
136141 $ alias = [$ extraAlias , $ alias ];
137142 }
138143
139144 $ aliasString = is_array ($ alias ) ? implode ('. ' , $ alias ) : $ alias ;
145+ $ useAlias = $ alias ? true : $ useAlias ;
140146
141147 $ relationJoinCache = $ alias
142148 ? "{$ aliasString }. {$ relationQuery ->getModel ()->getTable ()}. {$ relationName }"
@@ -146,21 +152,23 @@ public function joinRelationship(): Closure
146152 return $ this ;
147153 }
148154
155+ if ($ useAlias ) {
156+ StaticCache::setTableAliasForModel ($ relation ->getModel (), $ alias );
157+ }
149158
150159 $ joinHelper ->markRelationshipAsAlreadyJoined ($ this ->getModel (), $ relationJoinCache );
151160 StaticCache::clear ();
152161
153162 $ relation ->performJoinForEloquentPowerJoins (
154163 builder: $ this ,
155164 joinType: $ joinType ,
156- callback: $ callback ,
165+ callback: $ relationCallback ,
157166 alias: $ alias ,
158167 disableExtraConditions: $ disableExtraConditions ,
159168 morphable: $ morphable ,
160169 );
161170
162171 return $ this ;
163-
164172 };
165173 }
166174
@@ -353,7 +361,8 @@ public function orderByPowerJoins(): Closure
353361 }, $ this ->getModel ());
354362
355363 if ($ aggregation ) {
356- $ aliasName = sprintf ('%s_%s_%s ' ,
364+ $ aliasName = sprintf (
365+ '%s_%s_%s ' ,
357366 $ latestRelationshipModel ->getTable (),
358367 $ column ,
359368 $ aggregation
@@ -382,7 +391,6 @@ public function orderByPowerJoins(): Closure
382391 }
383392 return $ this ;
384393 };
385-
386394 }
387395
388396 public function orderByLeftPowerJoins (): Closure
@@ -517,7 +525,7 @@ public function hasNestedUsingJoins(): Closure
517525 foreach ($ relations as $ index => $ relation ) {
518526 $ relationName = $ relation ;
519527
520- if (! $ latestRelation ) {
528+ if (!$ latestRelation ) {
521529 $ relation = $ this ->getRelationWithoutConstraintsProxy ($ relation );
522530 } else {
523531 $ relation = $ latestRelation ->getModel ()->query ()->getRelationWithoutConstraintsProxy ($ relation );
@@ -540,7 +548,6 @@ public function powerJoinDoesntHave(): Closure
540548 return function ($ relation , $ boolean = 'and ' , Closure $ callback = null ) {
541549 return $ this ->powerJoinHas ($ relation , '< ' , 1 , $ boolean , $ callback );
542550 };
543-
544551 }
545552
546553 public function powerJoinWhereHas (): Closure
0 commit comments