Skip to content
This repository was archived by the owner on Apr 27, 2022. It is now read-only.

Commit 1bbaea3

Browse files
authored
Returnings with get allow passing options - where from now
1 parent 49747c0 commit 1bbaea3

File tree

1 file changed

+49
-17
lines changed

1 file changed

+49
-17
lines changed

src/Traits/ParentTreeModel.php

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -139,35 +139,57 @@ public function children() {
139139
/**
140140
* Returns a collection of direct children of
141141
* $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+
*
143148
* @return \Illuminate\Database\Eloquent\Collection
144149
*/
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+
147159
}
148160

149161
/**
150162
* 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+
*
152169
* @return \Illuminate\Database\Eloquent\Collection
153170
*/
154-
public function getDescendants() {
155-
$children = $this->getChildren();
171+
public function getDescendants($options = []) {
172+
$children = $this->getChildren($options);
156173

157174
foreach ($children as $item) {
158-
$item->setAttribute('children', $item->getDescendants());
175+
$item->setAttribute('children', $item->getDescendants($options));
159176
}
160177

161178
return $children;
162179
}
163180

164181
/**
165182
* 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+
*
167189
* @return boolean
168190
*/
169-
public function hasChildren() {
170-
return self::getChildren()->count() > 0;
191+
public function hasChildren($options = []) {
192+
return self::getChildren($options)->count() > 0;
171193
}
172194

173195
/**
@@ -205,7 +227,7 @@ public function scopeRootNodes($query) {
205227

206228
/**
207229
* Returns a collection with all the root nodes
208-
*
230+
*
209231
* @return \Illuminate\Database\Eloquent\Collection
210232
*/
211233
public static function getRootNodes() {
@@ -214,26 +236,36 @@ public static function getRootNodes() {
214236

215237
/**
216238
* 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+
*
218245
* @return \Illuminate\Database\Eloquent\Collection
219246
*/
220-
public static function getTree() {
247+
public static function getTree($options = []) {
221248
$roots = self::rootNodes()->get();
222249

223250
foreach ($roots as $item) {
224-
$item->setAttribute('children', $item->getDescendants());
251+
$item->setAttribute('children', $item->getDescendants($options));
225252
}
226253

227254
return $roots;
228255
}
229256

230257
/**
231258
* 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+
*
233265
* @return array
234266
*/
235-
public static function getTreeArray() {
236-
$col = self::getTree();
267+
public static function getTreeArray($options = []) {
268+
$col = self::getTree($options);
237269

238270
return self::treeToArray($col);
239271
}

0 commit comments

Comments
 (0)