@@ -189,7 +189,7 @@ protected function actionAppendOrPrepend(self $parent, $prepend = false)
189189 protected function setParent ($ value )
190190 {
191191 $ this ->setParentId ($ value ? $ value ->getKey () : null )
192- ->setRelation ('parent ' , $ value );
192+ ->setRelation ('parent ' , $ value );
193193
194194 return $ this ;
195195 }
@@ -230,7 +230,7 @@ public function refreshNode()
230230 public function parent ()
231231 {
232232 return $ this ->belongsTo (get_class ($ this ), $ this ->getParentIdName ())
233- ->setModel ($ this );
233+ ->setModel ($ this );
234234 }
235235
236236 /**
@@ -241,7 +241,7 @@ public function parent()
241241 public function children ()
242242 {
243243 return $ this ->hasMany (get_class ($ this ), $ this ->getParentIdName ())
244- ->setModel ($ this );
244+ ->setModel ($ this );
245245 }
246246
247247 /**
@@ -262,25 +262,25 @@ public function descendants()
262262 public function siblings ()
263263 {
264264 return $ this ->newScopedQuery ()
265- ->where ($ this ->getKeyName (), '<> ' , $ this ->getKey ())
266- ->where ($ this ->getParentIdName (), '= ' , $ this ->getParentId ());
265+ ->where ($ this ->getKeyName (), '<> ' , $ this ->getKey ())
266+ ->where ($ this ->getParentIdName (), '= ' , $ this ->getParentId ());
267267 }
268268
269- /**
269+ /**
270270 * Get the node siblings and the node itself.
271271 *
272272 * @return \Kalnoy\Nestedset\QueryBuilder
273273 */
274274 public function siblingsAndSelf ()
275275 {
276276 return $ this ->newScopedQuery ()
277- ->where ($ this ->getParentIdName (), '= ' , $ this ->getParentId ());
277+ ->where ($ this ->getParentIdName (), '= ' , $ this ->getParentId ());
278278 }
279279
280280 /**
281281 * Get query for the node siblings and the node itself.
282282 *
283- * @param array $columns
283+ * @param array $columns
284284 *
285285 * @return \Illuminate\Database\Eloquent\Collection
286286 */
@@ -297,7 +297,7 @@ public function getSiblingsAndSelf(array $columns = [ '*' ])
297297 public function nextSiblings ()
298298 {
299299 return $ this ->nextNodes ()
300- ->where ($ this ->getParentIdName (), '= ' , $ this ->getParentId ());
300+ ->where ($ this ->getParentIdName (), '= ' , $ this ->getParentId ());
301301 }
302302
303303 /**
@@ -308,7 +308,7 @@ public function nextSiblings()
308308 public function prevSiblings ()
309309 {
310310 return $ this ->prevNodes ()
311- ->where ($ this ->getParentIdName (), '= ' , $ this ->getParentId ());
311+ ->where ($ this ->getParentIdName (), '= ' , $ this ->getParentId ());
312312 }
313313
314314 /**
@@ -319,7 +319,7 @@ public function prevSiblings()
319319 public function nextNodes ()
320320 {
321321 return $ this ->newScopedQuery ()
322- ->where ($ this ->getLftName (), '> ' , $ this ->getLft ());
322+ ->where ($ this ->getLftName (), '> ' , $ this ->getLft ());
323323 }
324324
325325 /**
@@ -330,7 +330,7 @@ public function nextNodes()
330330 public function prevNodes ()
331331 {
332332 return $ this ->newScopedQuery ()
333- ->where ($ this ->getLftName (), '< ' , $ this ->getLft ());
333+ ->where ($ this ->getLftName (), '< ' , $ this ->getLft ());
334334 }
335335
336336 /**
@@ -426,7 +426,8 @@ public function prependToNode(self $parent)
426426 public function appendOrPrependTo (self $ parent , $ prepend = false )
427427 {
428428 $ this ->assertNodeExists ($ parent )
429- ->assertNotDescendant ($ parent );
429+ ->assertNotDescendant ($ parent )
430+ ->assertSameScope ($ parent );
430431
431432 $ this ->setParent ($ parent )->dirtyBounds ();
432433
@@ -465,7 +466,9 @@ public function beforeNode(self $node)
465466 */
466467 public function beforeOrAfterNode (self $ node , $ after = false )
467468 {
468- $ this ->assertNodeExists ($ node )->assertNotDescendant ($ node );
469+ $ this ->assertNodeExists ($ node )
470+ ->assertNotDescendant ($ node )
471+ ->assertSameScope ($ node );
469472
470473 if ( ! $ this ->isSiblingOf ($ node )) {
471474 $ this ->setParent ($ node ->getRelationValue ('parent ' ));
@@ -529,9 +532,9 @@ public function rawNode($lft, $rgt, $parentId)
529532 public function up ($ amount = 1 )
530533 {
531534 $ sibling = $ this ->prevSiblings ()
532- ->defaultOrder ('desc ' )
533- ->skip ($ amount - 1 )
534- ->first ();
535+ ->defaultOrder ('desc ' )
536+ ->skip ($ amount - 1 )
537+ ->first ();
535538
536539 if ( ! $ sibling ) return false ;
537540
@@ -548,9 +551,9 @@ public function up($amount = 1)
548551 public function down ($ amount = 1 )
549552 {
550553 $ sibling = $ this ->nextSiblings ()
551- ->defaultOrder ()
552- ->skip ($ amount - 1 )
553- ->first ();
554+ ->defaultOrder ()
555+ ->skip ($ amount - 1 )
556+ ->first ();
554557
555558 if ( ! $ sibling ) return false ;
556559
@@ -587,7 +590,7 @@ protected function insertAt($position)
587590 protected function moveNode ($ position )
588591 {
589592 $ updated = $ this ->newNestedSetQuery ()
590- ->moveNode ($ this ->getKey (), $ position ) > 0 ;
593+ ->moveNode ($ this ->getKey (), $ position ) > 0 ;
591594
592595 if ($ updated ) $ this ->refreshNode ();
593596
@@ -649,8 +652,8 @@ protected function deleteDescendants()
649652 protected function restoreDescendants ($ deletedAt )
650653 {
651654 $ this ->descendants ()
652- ->where ($ this ->getDeletedAtColumn (), '>= ' , $ deletedAt )
653- ->restore ();
655+ ->where ($ this ->getDeletedAtColumn (), '>= ' , $ deletedAt )
656+ ->restore ();
654657 }
655658
656659 /**
@@ -932,8 +935,8 @@ public function getPrevNode(array $columns = [ '*' ])
932935 public function getAncestors (array $ columns = [ '* ' ])
933936 {
934937 return $ this ->newScopedQuery ()
935- ->defaultOrder ()
936- ->ancestorsOf ($ this , $ columns );
938+ ->defaultOrder ()
939+ ->ancestorsOf ($ this , $ columns );
937940 }
938941
939942 /**
@@ -1006,7 +1009,7 @@ public function getPrevSibling(array $columns = [ '*' ])
10061009 public function isDescendantOf (self $ other )
10071010 {
10081011 return $ this ->getLft () > $ other ->getLft () &&
1009- $ this ->getLft () < $ other ->getRgt ();
1012+ $ this ->getLft () < $ other ->getRgt ();
10101013 }
10111014
10121015 /**
@@ -1019,7 +1022,7 @@ public function isDescendantOf(self $other)
10191022 public function isSelfOrDescendantOf (self $ other )
10201023 {
10211024 return $ this ->getLft () >= $ other ->getLft () &&
1022- $ this ->getLft () < $ other ->getRgt ();
1025+ $ this ->getLft () < $ other ->getRgt ();
10231026 }
10241027
10251028 /**
@@ -1186,4 +1189,19 @@ protected function assertNodeExists(self $node)
11861189 return $ this ;
11871190 }
11881191
1192+ /**
1193+ * @param self $node
1194+ */
1195+ protected function assertSameScope (self $ node )
1196+ {
1197+ if ( ! $ scoped = $ this ->getScopeAttributes ()) {
1198+ return ;
1199+ }
1200+
1201+ foreach ($ scoped as $ attr ) {
1202+ if ($ this ->getAttribute ($ attr ) != $ node ->getAttribute ($ attr )) {
1203+ throw new LogicException ('Nodes must be in the same scope ' );
1204+ }
1205+ }
1206+ }
11891207}
0 commit comments