@@ -87,8 +87,10 @@ public function whereIsRoot()
8787 public function whereAncestorOf ($ id , $ andSelf = false , $ boolean = 'and ' )
8888 {
8989 $ keyName = $ this ->model ->getTable () . '. ' . $ this ->model ->getKeyName ();
90+ $ model = null ;
9091
9192 if (NestedSet::isNode ($ id )) {
93+ $ model = $ id ;
9294 $ value = '? ' ;
9395
9496 $ this ->query ->addBinding ($ id ->getRgt ());
@@ -108,7 +110,7 @@ public function whereAncestorOf($id, $andSelf = false, $boolean = 'and')
108110 $ value = '( ' .$ valueQuery ->toSql ().') ' ;
109111 }
110112
111- $ this ->query ->whereNested (function ($ inner ) use ($ value , $ andSelf , $ id , $ keyName ) {
113+ $ this ->query ->whereNested (function ($ inner ) use ($ model , $ value , $ andSelf , $ id , $ keyName ) {
112114 list ($ lft , $ rgt ) = $ this ->wrappedColumns ();
113115 $ wrappedTable = $ this ->query ->getGrammar ()->wrapTable ($ this ->model ->getTable ());
114116
@@ -117,9 +119,13 @@ public function whereAncestorOf($id, $andSelf = false, $boolean = 'and')
117119 if ( ! $ andSelf ) {
118120 $ inner ->where ($ keyName , '<> ' , $ id );
119121 }
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+ }
120127 }, $ boolean );
121128
122-
123129 return $ this ;
124130 }
125131
@@ -178,12 +184,13 @@ public function ancestorsAndSelf($id, array $columns = [ '*' ])
178184 * @param array $values
179185 * @param string $boolean
180186 * @param bool $not
187+ * @param Query $query
181188 *
182189 * @return $this
183190 */
184- public function whereNodeBetween ($ values , $ boolean = 'and ' , $ not = false )
191+ public function whereNodeBetween ($ values , $ boolean = 'and ' , $ not = false , $ query = null )
185192 {
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 );
187194
188195 return $ this ;
189196 }
@@ -217,19 +224,26 @@ public function orWhereNodeBetween($values)
217224 public function whereDescendantOf ($ id , $ boolean = 'and ' , $ not = false ,
218225 $ andSelf = false
219226 ) {
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+ }
226237
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+ }
231242
232- return $ this ->whereNodeBetween ($ data , $ boolean , $ not );
243+ return $ this ->whereNodeBetween ($ data , 'and ' , $ not , $ inner );
244+ }, $ boolean );
245+
246+ return $ this ;
233247 }
234248
235249 /**
0 commit comments