@@ -76,7 +76,7 @@ public function children()
76
76
/**
77
77
* Get query for descendants of the node.
78
78
*
79
- * @return \Illuminate\Database\Eloquent\Builder
79
+ * @return \Kalnoy\Nestedset\QueryBuilder
80
80
*/
81
81
public function descendants ()
82
82
{
@@ -90,35 +90,38 @@ public function descendants()
90
90
*
91
91
* @param self::AFTER|self::BEFORE|null $dir
92
92
*
93
- * @return \Illuminate\Database\Eloquent\Builder
93
+ * @return \Kalnoy\Nestedset\QueryBuilder
94
94
*/
95
95
public function siblings ($ dir = null )
96
96
{
97
- $ query = $ this ->newQuery ();
98
-
99
- $ query ->where (static ::PARENT_ID , '= ' , $ this ->getParentId ());
100
-
101
97
switch ($ dir )
102
98
{
103
99
case self ::AFTER :
104
- $ query ->where (static ::LFT , '> ' , $ this ->getRgt ());
100
+ $ query = $ this ->next ();
101
+
105
102
break ;
106
103
107
104
case self ::BEFORE :
108
- $ query ->where (static ::LFT , '< ' , $ this ->getLft ());
105
+ $ query = $ this ->prev ();
106
+
109
107
break ;
110
108
111
109
default :
112
- $ query ->where ($ this ->getKeyName (), '<> ' , $ this ->getKey ());
110
+ $ query = $ this ->newQuery ()
111
+ ->where ($ this ->getKeyName (), '<> ' , $ this ->getKey ());
112
+
113
+ break ;
113
114
}
114
115
116
+ $ query ->where (static ::PARENT_ID , '= ' , $ this ->getParentId ());
117
+
115
118
return $ query ;
116
119
}
117
120
118
121
/**
119
122
* Get query for siblings after the node.
120
123
*
121
- * @return \Illuminate\Database\Eloquent\Builder
124
+ * @return \Kalnoy\Nestedset\QueryBuilder
122
125
*/
123
126
public function nextSiblings ()
124
127
{
@@ -128,17 +131,40 @@ public function nextSiblings()
128
131
/**
129
132
* Get query for siblings before the node.
130
133
*
131
- * @return \Illuminate\Database\Eloquent\Builder
134
+ * @return \Kalnoy\Nestedset\QueryBuilder
132
135
*/
133
136
public function prevSiblings ()
134
137
{
135
138
return $ this ->siblings (self ::BEFORE );
136
139
}
137
140
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
+
138
164
/**
139
165
* Get query for ancestors to the node not including the node itself.
140
166
*
141
- * @return \Illuminate\Database\Eloquent\Builder
167
+ * @return \Kalnoy\Nestedset\QueryBuilder
142
168
*/
143
169
public function ancestors ()
144
170
{
@@ -683,4 +709,100 @@ public function getParentId()
683
709
{
684
710
return $ this ->attributes [static ::PARENT_ID ];
685
711
}
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
+ }
686
808
}
0 commit comments