Skip to content

Commit

Permalink
Added setParents() to eager load the ->parent relation. This allows
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Hijmans authored and sdebacker committed Sep 30, 2020
1 parent 11bc91d commit 1714347
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/NestableCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,26 @@ public function getTotal()
{
return $this->total();
}

/**
* Sets the $item->parent relation for each item in the NestableCollection to be the parent it has in the collection
* so it can be used without querying the database.
*
* @return $this
*/
public function setParents()
{
$this->setParentsRecursive($this);
return $this;
}

protected function setParentsRecursive(&$items, &$parent = null)
{
foreach ($items as $item) {
if ($parent) {
$item->setRelation('parent', $parent);
}
$this->setParentsRecursive($item->items, $item);
}
}
}

0 comments on commit 1714347

Please sign in to comment.