@@ -19,17 +19,17 @@ class RelationshipsExtraMethods
1919 */
2020 public function performJoinForEloquentPowerJoins ()
2121 {
22- return function ($ builder , $ joinType = 'leftJoin ' , $ callback = null , $ alias = null ) {
22+ return function ($ builder , $ joinType = 'leftJoin ' , $ callback = null , $ alias = null , bool $ disableExtraConditions = false ) {
2323 if ($ this instanceof BelongsToMany) {
24- return $ this ->performJoinForEloquentPowerJoinsForBelongsToMany ($ builder , $ joinType , $ callback , $ alias );
24+ return $ this ->performJoinForEloquentPowerJoinsForBelongsToMany ($ builder , $ joinType , $ callback , $ alias, $ disableExtraConditions );
2525 } elseif ($ this instanceof MorphOneOrMany) {
26- $ this ->performJoinForEloquentPowerJoinsForMorph ($ builder , $ joinType , $ callback , $ alias );
26+ $ this ->performJoinForEloquentPowerJoinsForMorph ($ builder , $ joinType , $ callback , $ alias, $ disableExtraConditions );
2727 } elseif ($ this instanceof HasMany || $ this instanceof HasOne) {
28- return $ this ->performJoinForEloquentPowerJoinsForHasMany ($ builder , $ joinType , $ callback , $ alias );
28+ return $ this ->performJoinForEloquentPowerJoinsForHasMany ($ builder , $ joinType , $ callback , $ alias, $ disableExtraConditions );
2929 } elseif ($ this instanceof HasManyThrough) {
30- return $ this ->performJoinForEloquentPowerJoinsForHasManyThrough ($ builder , $ joinType , $ callback , $ alias );
30+ return $ this ->performJoinForEloquentPowerJoinsForHasManyThrough ($ builder , $ joinType , $ callback , $ alias, $ disableExtraConditions );
3131 } else {
32- return $ this ->performJoinForEloquentPowerJoinsForBelongsTo ($ builder , $ joinType , $ callback , $ alias );
32+ return $ this ->performJoinForEloquentPowerJoinsForBelongsTo ($ builder , $ joinType , $ callback , $ alias, $ disableExtraConditions );
3333 }
3434 };
3535 }
@@ -39,11 +39,11 @@ public function performJoinForEloquentPowerJoins()
3939 */
4040 protected function performJoinForEloquentPowerJoinsForBelongsTo ()
4141 {
42- return function ($ query , $ joinType , $ callback = null , $ alias = null ) {
42+ return function ($ query , $ joinType , $ callback = null , $ alias = null , bool $ disableExtraConditions = false ) {
4343 $ joinedTable = $ this ->query ->getModel ()->getTable ();
4444 $ parentTable = $ this ->getTableOrAliasForModel ($ this ->parent , $ this ->parent ->getTable ());
4545
46- $ query ->{$ joinType }($ joinedTable , function ($ join ) use ($ callback , $ joinedTable , $ parentTable , $ alias ) {
46+ $ query ->{$ joinType }($ joinedTable , function ($ join ) use ($ callback , $ joinedTable , $ parentTable , $ alias, $ disableExtraConditions ) {
4747 if ($ alias ) {
4848 $ join ->as ($ alias );
4949 }
@@ -54,11 +54,13 @@ protected function performJoinForEloquentPowerJoinsForBelongsTo()
5454 "{$ joinedTable }. {$ this ->ownerKey }"
5555 );
5656
57- if ($ this ->usesSoftDeletes ($ this ->query ->getModel ())) {
57+ if ($ disableExtraConditions === false && $ this ->usesSoftDeletes ($ this ->query ->getModel ())) {
5858 $ join ->whereNull ("{$ joinedTable }. {$ this ->query ->getModel ()->getDeletedAtColumn ()}" );
5959 }
6060
61- $ this ->applyExtraConditions ($ join );
61+ if ($ disableExtraConditions === false ) {
62+ $ this ->applyExtraConditions ($ join );
63+ }
6264
6365 if ($ callback && is_callable ($ callback )) {
6466 $ callback ($ join );
@@ -72,7 +74,7 @@ protected function performJoinForEloquentPowerJoinsForBelongsTo()
7274 */
7375 protected function performJoinForEloquentPowerJoinsForBelongsToMany ()
7476 {
75- return function ($ builder , $ joinType , $ callback = null , $ alias = null ) {
77+ return function ($ builder , $ joinType , $ callback = null , $ alias = null , bool $ disableExtraConditions = false ) {
7678 [$ alias1 , $ alias2 ] = $ alias ;
7779
7880 $ joinedTable = $ alias1 ?: $ this ->getTable ();
@@ -94,7 +96,7 @@ protected function performJoinForEloquentPowerJoinsForBelongsToMany()
9496 }
9597 });
9698
97- $ builder ->{$ joinType }($ this ->getModel ()->getTable (), function ($ join ) use ($ callback , $ joinedTable , $ alias2 ) {
99+ $ builder ->{$ joinType }($ this ->getModel ()->getTable (), function ($ join ) use ($ callback , $ joinedTable , $ alias2, $ disableExtraConditions ) {
98100 if ($ alias2 ) {
99101 $ join ->as ($ alias2 );
100102 }
@@ -105,12 +107,14 @@ protected function performJoinForEloquentPowerJoinsForBelongsToMany()
105107 "{$ joinedTable }. {$ this ->getRelatedPivotKeyName ()}"
106108 );
107109
108- if ($ this ->usesSoftDeletes ($ this ->query ->getModel ())) {
110+ if ($ disableExtraConditions === false && $ this ->usesSoftDeletes ($ this ->query ->getModel ())) {
109111 $ join ->whereNull ($ this ->query ->getModel ()->getQualifiedDeletedAtColumn ());
110112 }
111113
112114 // applying any extra conditions to the belongs to many relationship
113- $ this ->applyExtraConditions ($ join );
115+ if ($ disableExtraConditions === false ) {
116+ $ this ->applyExtraConditions ($ join );
117+ }
114118
115119 if (is_array ($ callback ) && isset ($ callback [$ this ->getModel ()->getTable ()])) {
116120 $ callback [$ this ->getModel ()->getTable ()]($ join );
@@ -126,19 +130,21 @@ protected function performJoinForEloquentPowerJoinsForBelongsToMany()
126130 */
127131 protected function performJoinForEloquentPowerJoinsForMorph ()
128132 {
129- return function ($ builder , $ joinType , $ callback = null ) {
130- $ builder ->{$ joinType }($ this ->getModel ()->getTable (), function ($ join ) use ($ callback ) {
133+ return function ($ builder , $ joinType , $ callback = null , $ alias = null , bool $ disableExtraConditions = false ) {
134+ $ builder ->{$ joinType }($ this ->getModel ()->getTable (), function ($ join ) use ($ callback, $ disableExtraConditions ) {
131135 $ join ->on (
132136 "{$ this ->getModel ()->getTable ()}. {$ this ->getForeignKeyName ()}" ,
133137 '= ' ,
134138 "{$ this ->parent ->getTable ()}. {$ this ->localKey }"
135139 )->where ($ this ->getMorphType (), '= ' , $ this ->getMorphClass ());
136140
137- if ($ this ->usesSoftDeletes ($ this ->query ->getModel ())) {
141+ if ($ disableExtraConditions === false && $ this ->usesSoftDeletes ($ this ->query ->getModel ())) {
138142 $ join ->whereNull ($ this ->query ->getModel ()->getQualifiedDeletedAtColumn ());
139143 }
140144
141- $ this ->applyExtraConditions ($ join );
145+ if ($ disableExtraConditions === false ) {
146+ $ this ->applyExtraConditions ($ join );
147+ }
142148
143149 if ($ callback && is_callable ($ callback )) {
144150 $ callback ($ join );
@@ -154,11 +160,11 @@ protected function performJoinForEloquentPowerJoinsForMorph()
154160 */
155161 protected function performJoinForEloquentPowerJoinsForHasMany ()
156162 {
157- return function ($ builder , $ joinType , $ callback = null , $ alias = null ) {
163+ return function ($ builder , $ joinType , $ callback = null , $ alias = null , bool $ disableExtraConditions = false ) {
158164 $ joinedTable = $ alias ?: $ this ->query ->getModel ()->getTable ();
159165 $ parentTable = $ this ->getTableOrAliasForModel ($ this ->parent , $ this ->parent ->getTable ());
160166
161- $ builder ->{$ joinType }($ this ->query ->getModel ()->getTable (), function ($ join ) use ($ callback , $ joinedTable , $ parentTable , $ alias ) {
167+ $ builder ->{$ joinType }($ this ->query ->getModel ()->getTable (), function ($ join ) use ($ callback , $ joinedTable , $ parentTable , $ alias, $ disableExtraConditions ) {
162168 if ($ alias ) {
163169 $ join ->as ($ alias );
164170 }
@@ -169,13 +175,15 @@ protected function performJoinForEloquentPowerJoinsForHasMany()
169175 "{$ parentTable }. {$ this ->localKey }"
170176 );
171177
172- if ($ this ->usesSoftDeletes ($ this ->query ->getModel ())) {
178+ if ($ disableExtraConditions === false && $ this ->usesSoftDeletes ($ this ->query ->getModel ())) {
173179 $ join ->whereNull (
174180 "{$ joinedTable }. {$ this ->query ->getModel ()->getDeletedAtColumn ()}"
175181 );
176182 }
177183
178- $ this ->applyExtraConditions ($ join );
184+ if ($ disableExtraConditions === false ) {
185+ $ this ->applyExtraConditions ($ join );
186+ }
179187
180188 if ($ callback && is_callable ($ callback )) {
181189 $ callback ($ join );
@@ -189,12 +197,12 @@ protected function performJoinForEloquentPowerJoinsForHasMany()
189197 */
190198 protected function performJoinForEloquentPowerJoinsForHasManyThrough ()
191199 {
192- return function ($ builder , $ joinType , $ callback = null , $ alias = null ) {
200+ return function ($ builder , $ joinType , $ callback = null , $ alias = null , bool $ disableExtraConditions = false ) {
193201 [$ alias1 , $ alias2 ] = $ alias ;
194202 $ throughTable = $ alias1 ?: $ this ->getThroughParent ()->getTable ();
195203 $ farTable = $ alias2 ?: $ this ->getModel ()->getTable ();
196204
197- $ builder ->{$ joinType }($ this ->getThroughParent ()->getTable (), function (PowerJoinClause $ join ) use ($ callback , $ throughTable , $ alias1 ) {
205+ $ builder ->{$ joinType }($ this ->getThroughParent ()->getTable (), function (PowerJoinClause $ join ) use ($ callback , $ throughTable , $ alias1, $ disableExtraConditions ) {
198206 if ($ alias1 ) {
199207 $ join ->as ($ alias1 );
200208 }
@@ -205,10 +213,14 @@ protected function performJoinForEloquentPowerJoinsForHasManyThrough()
205213 $ this ->getQualifiedLocalKeyName ()
206214 );
207215
208- if ($ this ->usesSoftDeletes ($ this ->getThroughParent ())) {
216+ if ($ disableExtraConditions === false && $ this ->usesSoftDeletes ($ this ->getThroughParent ())) {
209217 $ join ->whereNull ($ this ->getThroughParent ()->getQualifiedDeletedAtColumn ());
210218 }
211219
220+ if ($ disableExtraConditions === false ) {
221+ $ this ->applyExtraConditions ($ join );
222+ }
223+
212224 if (is_array ($ callback ) && isset ($ callback [$ this ->getThroughParent ()->getTable ()])) {
213225 $ callback [$ this ->getThroughParent ()->getTable ()]($ join );
214226 }
@@ -350,6 +362,10 @@ public function getPowerJoinExistenceCompareKey()
350362 return $ this ->getExistenceCompareKey ();
351363 }
352364
365+ if ($ this instanceof HasManyThrough) {
366+ return $ this ->getQualifiedFirstKeyName ();
367+ }
368+
353369 if ($ this instanceof BelongsToMany) {
354370 return $ this ->getExistenceCompareKey ();
355371 }
0 commit comments