@@ -98,22 +98,32 @@ public function joinRelationship(): Closure
9898 bool $ useAlias = false ,
9999 bool $ disableExtraConditions = false ,
100100 ?string $ morphable = null ,
101+ ?string $ initialModel = null ,
101102 ) {
102103 $ joinType = JoinsHelper::$ joinMethodsMap [$ joinType ] ?? $ joinType ;
103104 $ useAlias = is_string ($ callback ) ? false : $ useAlias ;
104- $ joinHelper = JoinsHelper::make ($ this ->getModel ());
105+ $ currentModel = $ initialModel ? (new $ initialModel ) : $ this ->getModel ();
106+ $ joinHelper = JoinsHelper::make ($ currentModel );
105107 $ callback = $ joinHelper ->formatJoinCallback ($ callback );
106108
107109 $ this ->getQuery ()->beforeQuery (function () use ($ joinHelper ) {
108110 $ joinHelper ->clear ();
109111 });
110112
111113 if (is_null ($ this ->getSelect ())) {
112- $ this ->select (sprintf ('%s.* ' , $ this -> getModel () ->getTable ()));
114+ $ this ->select (sprintf ('%s.* ' , $ currentModel ->getTable ()));
113115 }
114116
115117 if (Str::contains ($ relationName , '. ' )) {
116- $ this ->joinNestedRelationship ($ relationName , $ callback , $ joinType , $ useAlias , $ disableExtraConditions , $ morphable );
118+ $ this ->joinNestedRelationship (
119+ $ relationName ,
120+ $ callback ,
121+ $ joinType ,
122+ $ useAlias ,
123+ $ disableExtraConditions ,
124+ $ morphable ,
125+ $ initialModel
126+ );
117127
118128 return $ this ;
119129 }
@@ -123,7 +133,7 @@ public function joinRelationship(): Closure
123133 $ relationCallback = $ callback [$ relationName ];
124134 }
125135
126- $ relation = $ this -> getModel () ->{$ relationName }();
136+ $ relation = $ currentModel ->{$ relationName }();
127137 $ relationQuery = $ relation ->getQuery ();
128138 $ alias = $ joinHelper ->getAliasName (
129139 $ useAlias ,
@@ -151,15 +161,15 @@ public function joinRelationship(): Closure
151161 ? "{$ aliasString }. {$ relationQuery ->getModel ()->getTable ()}. {$ relationName }"
152162 : "{$ relationQuery ->getModel ()->getTable ()}. {$ relationName }" ;
153163
154- if ($ joinHelper ->relationshipAlreadyJoined ($ this -> getModel () , $ relationJoinCache )) {
164+ if ($ joinHelper ->relationshipAlreadyJoined ($ currentModel , $ relationJoinCache )) {
155165 return $ this ;
156166 }
157167
158168 if ($ useAlias ) {
159169 StaticCache::setTableAliasForModel ($ relation ->getModel (), $ alias );
160170 }
161171
162- $ joinHelper ->markRelationshipAsAlreadyJoined ($ this -> getModel () , $ relationJoinCache );
172+ $ joinHelper ->markRelationshipAsAlreadyJoined ($ currentModel , $ relationJoinCache );
163173 StaticCache::clear ();
164174
165175 $ relation ->performJoinForEloquentPowerJoins (
@@ -259,9 +269,11 @@ public function joinNestedRelationship(): Closure
259269 bool $ useAlias = false ,
260270 bool $ disableExtraConditions = false ,
261271 ?string $ morphable = null ,
272+ ?string $ initialModel = null ,
262273 ) {
263274 $ relations = explode ('. ' , $ relationships );
264- $ joinHelper = JoinsHelper::make ($ this ->getModel ());
275+ $ currentModel = $ initialModel ? (new $ initialModel ) : $ this ->getModel ();
276+ $ joinHelper = JoinsHelper::make ($ currentModel );
265277 /** @var Relation */
266278 $ latestRelation = null ;
267279
@@ -270,7 +282,7 @@ public function joinNestedRelationship(): Closure
270282 $ part [] = $ relationName ;
271283 $ fullRelationName = join ('. ' , $ part );
272284
273- $ currentModel = $ latestRelation ? $ latestRelation ->getModel () : $ this -> getModel () ;
285+ $ currentModel = $ latestRelation ? $ latestRelation ->getModel () : $ currentModel ;
274286 $ relation = $ currentModel ->{$ relationName }();
275287 $ relationCallback = null ;
276288
@@ -319,7 +331,7 @@ public function joinNestedRelationship(): Closure
319331 StaticCache::setTableAliasForModel ($ relation ->getModel (), $ alias );
320332 }
321333
322- if ($ joinHelper ->relationshipAlreadyJoined ($ this -> getModel () , $ relationJoinCache )) {
334+ if ($ joinHelper ->relationshipAlreadyJoined ($ currentModel , $ relationJoinCache )) {
323335 $ latestRelation = $ relation ;
324336
325337 continue ;
@@ -335,7 +347,7 @@ public function joinNestedRelationship(): Closure
335347 );
336348
337349 $ latestRelation = $ relation ;
338- $ joinHelper ->markRelationshipAsAlreadyJoined ($ this -> getModel () , $ relationJoinCache );
350+ $ joinHelper ->markRelationshipAsAlreadyJoined ($ currentModel , $ relationJoinCache );
339351 }
340352
341353 StaticCache::clear ();
0 commit comments