@@ -76,7 +76,7 @@ public function children()
7676 /**
7777 * Get query for descendants of the node.
7878 *
79- * @return \Illuminate\Database\Eloquent\Builder
79+ * @return \Kalnoy\Nestedset\QueryBuilder
8080 */
8181 public function descendants ()
8282 {
@@ -90,35 +90,38 @@ public function descendants()
9090 *
9191 * @param self::AFTER|self::BEFORE|null $dir
9292 *
93- * @return \Illuminate\Database\Eloquent\Builder
93+ * @return \Kalnoy\Nestedset\QueryBuilder
9494 */
9595 public function siblings ($ dir = null )
9696 {
97- $ query = $ this ->newQuery ();
98-
99- $ query ->where (static ::PARENT_ID , '= ' , $ this ->getParentId ());
100-
10197 switch ($ dir )
10298 {
10399 case self ::AFTER :
104- $ query ->where (static ::LFT , '> ' , $ this ->getRgt ());
100+ $ query = $ this ->next ();
101+
105102 break ;
106103
107104 case self ::BEFORE :
108- $ query ->where (static ::LFT , '< ' , $ this ->getLft ());
105+ $ query = $ this ->prev ();
106+
109107 break ;
110108
111109 default :
112- $ query ->where ($ this ->getKeyName (), '<> ' , $ this ->getKey ());
110+ $ query = $ this ->newQuery ()
111+ ->where ($ this ->getKeyName (), '<> ' , $ this ->getKey ());
112+
113+ break ;
113114 }
114115
116+ $ query ->where (static ::PARENT_ID , '= ' , $ this ->getParentId ());
117+
115118 return $ query ;
116119 }
117120
118121 /**
119122 * Get query for siblings after the node.
120123 *
121- * @return \Illuminate\Database\Eloquent\Builder
124+ * @return \Kalnoy\Nestedset\QueryBuilder
122125 */
123126 public function nextSiblings ()
124127 {
@@ -128,17 +131,40 @@ public function nextSiblings()
128131 /**
129132 * Get query for siblings before the node.
130133 *
131- * @return \Illuminate\Database\Eloquent\Builder
134+ * @return \Kalnoy\Nestedset\QueryBuilder
132135 */
133136 public function prevSiblings ()
134137 {
135138 return $ this ->siblings (self ::BEFORE );
136139 }
137140
141+ /**
142+ * Get query for nodes after current node.
143+ *
144+ * @return \Kalnoy\Nestedset\QueryBuilder
145+ */
146+ public function next ()
147+ {
148+ return $ this ->newQuery ()
149+ ->where (static ::LFT , '> ' , $ this ->attributes [static ::LFT ]);
150+ }
151+
152+ /**
153+ * Get query for nodes before current node in reversed order.
154+ *
155+ * @return \Kalnoy\Nestedset\QueryBuilder
156+ */
157+ public function prev ()
158+ {
159+ return $ this ->newQuery ()
160+ ->where (static ::LFT , '< ' , $ this ->attributes [static ::LFT ])
161+ ->reversed ();
162+ }
163+
138164 /**
139165 * Get query for ancestors to the node not including the node itself.
140166 *
141- * @return \Illuminate\Database\Eloquent\Builder
167+ * @return \Kalnoy\Nestedset\QueryBuilder
142168 */
143169 public function ancestors ()
144170 {
@@ -683,4 +709,100 @@ public function getParentId()
683709 {
684710 return $ this ->attributes [static ::PARENT_ID ];
685711 }
712+
713+ /**
714+ * Shorthand for next()
715+ *
716+ * @param array $columns
717+ *
718+ * @return \Kalnoy\Nestedset\Node
719+ */
720+ public function getNext (array $ columns = array ('* ' ))
721+ {
722+ return $ this ->next ()->first ($ columns );
723+ }
724+
725+ /**
726+ * Shorthand for prev()
727+ *
728+ * @param array $columns
729+ *
730+ * @return \Kalnoy\Nestedset\Node
731+ */
732+ public function getPrev (array $ columns = array ('* ' ))
733+ {
734+ return $ this ->prev ()->first ($ columns );
735+ }
736+
737+ /**
738+ * Shorthand for ancestors()
739+ *
740+ * @param array $columns
741+ *
742+ * @return \Kalnoy\Nestedset\Collection
743+ */
744+ public function getAncestors (array $ columns = array ('* ' ))
745+ {
746+ return $ this ->ancestors ()->get ($ columns );
747+ }
748+
749+ /**
750+ * Shorthand for descendants()
751+ *
752+ * @param array $columns
753+ *
754+ * @return \Kalnoy\Nestedset\Collection
755+ */
756+ public function getDescendants (array $ columns = array ('* ' ))
757+ {
758+ return $ this ->descendants ()->get ($ columns );
759+ }
760+
761+ /**
762+ * Shorthand for nextSiblings().
763+ *
764+ * @param array $columns
765+ *
766+ * @return \Kalnoy\Nestedset\Collection
767+ */
768+ public function getNextSiblings (array $ columns = array ('* ' ))
769+ {
770+ return $ this ->nextSiblings ()->get ($ columns );
771+ }
772+
773+ /**
774+ * Shorthand for prevSiblings().
775+ *
776+ * @param array $columns
777+ *
778+ * @return \Kalnoy\Nestedset\Collection
779+ */
780+ public function getPrevSiblings (array $ columns = array ('* ' ))
781+ {
782+ return $ this ->prevSiblings ()->get ($ columns );
783+ }
784+
785+ /**
786+ * Get next sibling.
787+ *
788+ * @param array $columns
789+ *
790+ * @return \Kalnoy\Nestedset\Node
791+ */
792+ public function getNextSibling (array $ columns = array ('* ' ))
793+ {
794+ return $ this ->nextSiblings ()->first ($ columns );
795+ }
796+
797+ /**
798+ * Get previous sibling.
799+ *
800+ * @param array $columns
801+ *
802+ * @return \Kalnoy\Nestedset\Node
803+ */
804+ public function getPrevSibling (array $ columns = array ('* ' ))
805+ {
806+ return $ this ->prevSiblings ()->reversed ()->first ($ columns );
807+ }
686808}
0 commit comments