@@ -87,8 +87,10 @@ public function whereIsRoot()
87
87
public function whereAncestorOf ($ id , $ andSelf = false , $ boolean = 'and ' )
88
88
{
89
89
$ keyName = $ this ->model ->getTable () . '. ' . $ this ->model ->getKeyName ();
90
+ $ model = null ;
90
91
91
92
if (NestedSet::isNode ($ id )) {
93
+ $ model = $ id ;
92
94
$ value = '? ' ;
93
95
94
96
$ this ->query ->addBinding ($ id ->getRgt ());
@@ -108,7 +110,7 @@ public function whereAncestorOf($id, $andSelf = false, $boolean = 'and')
108
110
$ value = '( ' .$ valueQuery ->toSql ().') ' ;
109
111
}
110
112
111
- $ this ->query ->whereNested (function ($ inner ) use ($ value , $ andSelf , $ id , $ keyName ) {
113
+ $ this ->query ->whereNested (function ($ inner ) use ($ model , $ value , $ andSelf , $ id , $ keyName ) {
112
114
list ($ lft , $ rgt ) = $ this ->wrappedColumns ();
113
115
$ wrappedTable = $ this ->query ->getGrammar ()->wrapTable ($ this ->model ->getTable ());
114
116
@@ -117,9 +119,13 @@ public function whereAncestorOf($id, $andSelf = false, $boolean = 'and')
117
119
if ( ! $ andSelf ) {
118
120
$ inner ->where ($ keyName , '<> ' , $ id );
119
121
}
122
+ if ($ model !== null ) {
123
+ // we apply scope only when Node was passed as $id.
124
+ // In other cases, according to docs, query should be scoped() before calling this method
125
+ $ model ->applyNestedSetScope ($ inner );
126
+ }
120
127
}, $ boolean );
121
128
122
-
123
129
return $ this ;
124
130
}
125
131
@@ -178,12 +184,13 @@ public function ancestorsAndSelf($id, array $columns = [ '*' ])
178
184
* @param array $values
179
185
* @param string $boolean
180
186
* @param bool $not
187
+ * @param Query $query
181
188
*
182
189
* @return $this
183
190
*/
184
- public function whereNodeBetween ($ values , $ boolean = 'and ' , $ not = false )
191
+ public function whereNodeBetween ($ values , $ boolean = 'and ' , $ not = false , $ query = null )
185
192
{
186
- $ this ->query ->whereBetween ($ this ->model ->getTable () . '. ' . $ this ->model ->getLftName (), $ values , $ boolean , $ not );
193
+ ( $ query ?? $ this ->query ) ->whereBetween ($ this ->model ->getTable () . '. ' . $ this ->model ->getLftName (), $ values , $ boolean , $ not );
187
194
188
195
return $ this ;
189
196
}
@@ -217,19 +224,26 @@ public function orWhereNodeBetween($values)
217
224
public function whereDescendantOf ($ id , $ boolean = 'and ' , $ not = false ,
218
225
$ andSelf = false
219
226
) {
220
- if (NestedSet::isNode ($ id )) {
221
- $ data = $ id ->getBounds ();
222
- } else {
223
- $ data = $ this ->model ->newNestedSetQuery ()
224
- ->getPlainNodeData ($ id , true );
225
- }
227
+ $ this ->query ->whereNested (function (Query $ inner ) use ($ id , $ andSelf , $ not ) {
228
+ if (NestedSet::isNode ($ id )) {
229
+ $ id ->applyNestedSetScope ($ inner );
230
+ $ data = $ id ->getBounds ();
231
+ } else {
232
+ // we apply scope only when Node was passed as $id.
233
+ // In other cases, according to docs, query should be scoped() before calling this method
234
+ $ data = $ this ->model ->newNestedSetQuery ()
235
+ ->getPlainNodeData ($ id , true );
236
+ }
226
237
227
- // Don't include the node
228
- if ( ! $ andSelf ) {
229
- ++$ data [0 ];
230
- }
238
+ // Don't include the node
239
+ if (! $ andSelf ) {
240
+ ++$ data [0 ];
241
+ }
231
242
232
- return $ this ->whereNodeBetween ($ data , $ boolean , $ not );
243
+ return $ this ->whereNodeBetween ($ data , 'and ' , $ not , $ inner );
244
+ }, $ boolean );
245
+
246
+ return $ this ;
233
247
}
234
248
235
249
/**
0 commit comments