@@ -139,35 +139,57 @@ public function children() {
139
139
/**
140
140
* Returns a collection of direct children of
141
141
* $this model id.
142
- *
142
+ *
143
+ * @param array $options An array with options to be appplied when recovering descendants
144
+ * <ul>
145
+ * <li>where => [key, value] - recover only descendants the key matches the value
146
+ * </ul>
147
+ *
143
148
* @return \Illuminate\Database\Eloquent\Collection
144
149
*/
145
- public function getChildren () {
146
- return self ::where ($ this ->parentField , $ this ->getAttribute ($ this ->getParentIdField ()))->get ();
150
+ public function getChildren ($ options = []) {
151
+ $ children = self ::where ($ this ->parentField , $ this ->getAttribute ($ this ->getParentIdField ()))->get ();
152
+
153
+ if (array_key_exists ('where ' , $ options )) {
154
+ $ children = $ children ->where ($ options ['where ' ][0 ], $ options ['where ' ][1 ]);
155
+ }
156
+
157
+ return $ children ;
158
+
147
159
}
148
160
149
161
/**
150
162
* Returns a collection with all descendants of current Model
151
- *
163
+ *
164
+ * @param array $options An array with options to be appplied when recovering descendants
165
+ * <ul>
166
+ * <li>where => [key, value] - recover only descendants the key matches the value
167
+ * </ul>
168
+ *
152
169
* @return \Illuminate\Database\Eloquent\Collection
153
170
*/
154
- public function getDescendants () {
155
- $ children = $ this ->getChildren ();
171
+ public function getDescendants ($ options = [] ) {
172
+ $ children = $ this ->getChildren ($ options );
156
173
157
174
foreach ($ children as $ item ) {
158
- $ item ->setAttribute ('children ' , $ item ->getDescendants ());
175
+ $ item ->setAttribute ('children ' , $ item ->getDescendants ($ options ));
159
176
}
160
177
161
178
return $ children ;
162
179
}
163
180
164
181
/**
165
182
* Returns true if the model has direct children, false if not
166
- *
183
+ *
184
+ * @param array $options An array with options to be appplied when recovering descendants
185
+ * <ul>
186
+ * <li>where => [key, value] - recover only descendants the key matches the value
187
+ * </ul>
188
+ *
167
189
* @return boolean
168
190
*/
169
- public function hasChildren () {
170
- return self ::getChildren ()->count () > 0 ;
191
+ public function hasChildren ($ options = [] ) {
192
+ return self ::getChildren ($ options )->count () > 0 ;
171
193
}
172
194
173
195
/**
@@ -205,7 +227,7 @@ public function scopeRootNodes($query) {
205
227
206
228
/**
207
229
* Returns a collection with all the root nodes
208
- *
230
+ *
209
231
* @return \Illuminate\Database\Eloquent\Collection
210
232
*/
211
233
public static function getRootNodes () {
@@ -214,26 +236,36 @@ public static function getRootNodes() {
214
236
215
237
/**
216
238
* Returns the full tree from database
217
- *
239
+ *
240
+ * @param array $options An array with options to be appplied when recovering descendants
241
+ * <ul>
242
+ * <li>where => [key, value] - recover only descendants the key matches the value
243
+ * </ul>
244
+ *
218
245
* @return \Illuminate\Database\Eloquent\Collection
219
246
*/
220
- public static function getTree () {
247
+ public static function getTree ($ options = [] ) {
221
248
$ roots = self ::rootNodes ()->get ();
222
249
223
250
foreach ($ roots as $ item ) {
224
- $ item ->setAttribute ('children ' , $ item ->getDescendants ());
251
+ $ item ->setAttribute ('children ' , $ item ->getDescendants ($ options ));
225
252
}
226
253
227
254
return $ roots ;
228
255
}
229
256
230
257
/**
231
258
* Returns the full tree from database as a nested array
232
- *
259
+ *
260
+ * @param array $options An array with options to be appplied when recovering descendants
261
+ * <ul>
262
+ * <li>where => [key, value] - recover only descendants the key matches the value
263
+ * </ul>
264
+ *
233
265
* @return array
234
266
*/
235
- public static function getTreeArray () {
236
- $ col = self ::getTree ();
267
+ public static function getTreeArray ($ options = [] ) {
268
+ $ col = self ::getTree ($ options );
237
269
238
270
return self ::treeToArray ($ col );
239
271
}
0 commit comments