1212use Illuminate \Database \Eloquent \Relations \BelongsToMany ;
1313use Illuminate \Database \Eloquent \Relations \HasManyThrough ;
1414use Illuminate \Database \Eloquent \Relations \MorphOneOrMany ;
15+ use Illuminate \Database \Eloquent \Relations \MorphToMany ;
1516
1617/**
1718 * @method getModel
@@ -25,10 +26,12 @@ class RelationshipsExtraMethods
2526 public function performJoinForEloquentPowerJoins ()
2627 {
2728 return function ($ builder , $ joinType = 'leftJoin ' , $ callback = null , $ alias = null , bool $ disableExtraConditions = false ) {
28- if ($ this instanceof BelongsToMany) {
29+ if ($ this instanceof MorphToMany) {
30+ return $ this ->performJoinForEloquentPowerJoinsForMorphToMany ($ builder , $ joinType , $ callback , $ alias , $ disableExtraConditions );
31+ } elseif ($ this instanceof BelongsToMany) {
2932 return $ this ->performJoinForEloquentPowerJoinsForBelongsToMany ($ builder , $ joinType , $ callback , $ alias , $ disableExtraConditions );
3033 } elseif ($ this instanceof MorphOneOrMany) {
31- $ this ->performJoinForEloquentPowerJoinsForMorph ($ builder , $ joinType , $ callback , $ alias , $ disableExtraConditions );
34+ return $ this ->performJoinForEloquentPowerJoinsForMorph ($ builder , $ joinType , $ callback , $ alias , $ disableExtraConditions );
3235 } elseif ($ this instanceof HasMany || $ this instanceof HasOne) {
3336 return $ this ->performJoinForEloquentPowerJoinsForHasMany ($ builder , $ joinType , $ callback , $ alias , $ disableExtraConditions );
3437 } elseif ($ this instanceof HasManyThrough) {
@@ -130,6 +133,62 @@ protected function performJoinForEloquentPowerJoinsForBelongsToMany()
130133 };
131134 }
132135
136+ /**
137+ * Perform the JOIN clause for the MorphToMany (or similar) relationships.
138+ */
139+ protected function performJoinForEloquentPowerJoinsForMorphToMany ()
140+ {
141+ return function ($ builder , $ joinType , $ callback = null , $ alias = null , bool $ disableExtraConditions = false ) {
142+ [$ alias1 , $ alias2 ] = $ alias ;
143+
144+ $ joinedTable = $ alias1 ?: $ this ->getTable ();
145+ $ parentTable = $ this ->getTableOrAliasForModel ($ this ->parent ) ?? $ this ->parent ->getTable ();
146+
147+ $ builder ->{$ joinType }($ this ->getTable (), function ($ join ) use ($ callback , $ joinedTable , $ parentTable , $ alias1 , $ disableExtraConditions ) {
148+ if ($ alias1 ) {
149+ $ join ->as ($ alias1 );
150+ }
151+
152+ $ join ->on (
153+ "{$ joinedTable }. {$ this ->getForeignPivotKeyName ()}" ,
154+ '= ' ,
155+ "{$ parentTable }. {$ this ->parentKey }"
156+ );
157+
158+ // applying any extra conditions to the belongs to many relationship
159+ if ($ disableExtraConditions === false ) {
160+ $ this ->applyExtraConditions ($ join );
161+ }
162+
163+ if (is_array ($ callback ) && isset ($ callback [$ this ->getTable ()])) {
164+ $ callback [$ this ->getTable ()]($ join );
165+ }
166+ });
167+
168+ $ builder ->{$ joinType }($ this ->getModel ()->getTable (), function ($ join ) use ($ callback , $ joinedTable , $ alias2 , $ disableExtraConditions ) {
169+ if ($ alias2 ) {
170+ $ join ->as ($ alias2 );
171+ }
172+
173+ $ join ->on (
174+ "{$ this ->getModel ()->getTable ()}. {$ this ->getModel ()->getKeyName ()}" ,
175+ '= ' ,
176+ "{$ joinedTable }. {$ this ->getRelatedPivotKeyName ()}"
177+ );
178+
179+ if ($ disableExtraConditions === false && $ this ->usesSoftDeletes ($ this ->query ->getModel ())) {
180+ $ join ->whereNull ($ this ->query ->getModel ()->getQualifiedDeletedAtColumn ());
181+ }
182+
183+ if (is_array ($ callback ) && isset ($ callback [$ this ->getModel ()->getTable ()])) {
184+ $ callback [$ this ->getModel ()->getTable ()]($ join );
185+ }
186+ }, $ this ->getModel ());
187+
188+ return $ this ;
189+ };
190+ }
191+
133192 /**
134193 * Perform the JOIN clause for the Morph (or similar) relationships.
135194 */
@@ -316,7 +375,7 @@ public function applyExtraConditions()
316375 continue ;
317376 }
318377
319- if (! in_array ($ condition ['type ' ], ['Basic ' , 'Null ' , 'NotNull ' , 'Nested ' ])) {
378+ if (!in_array ($ condition ['type ' ], ['Basic ' , 'Null ' , 'NotNull ' , 'Nested ' ])) {
320379 continue ;
321380 }
322381
0 commit comments