Menu\Items\ItemList::hydrate PHP Method

hydrate() public method

Menu::hydrate(function($parentId) { return Page::where('parent_id', $parentId) ->get(); }, function($children, $page) { $children->add($page->slug, $page->name); });
public hydrate ( Closure $resolver, Closure $decorator, integer $idField = 'id', $parentIdField = 'parent_id', integer $parentId ) : ItemList
$resolver Closure the callback to resolve results for a given parentId
$decorator Closure the callback that modifies the ItemList for the given node
$idField integer the id column that matches with the parentId
$parentId integer the parentId to start hydrating from
return ItemList the
    public function hydrate($resolver, $decorator, $idField = 'id', $parentIdField = 'parent_id', $parentId = 0)
    {
        $items = is_callable($resolver) ? $resolver() : $resolver;
        if ($items instanceof Collection) {
            $items = $items->all();
        }
        $itemsForThisLevel = array_filter($items, function ($item) use($parentId, $parentIdField) {
            return $parentId == (is_object($item) ? isset($item->{$parentIdField}) ? $item->{$parentIdField} : 0 : (isset($item[$parentIdField]) ? $item[$parentIdField] : 0));
        });
        foreach ($itemsForThisLevel as $item) {
            // Let the decorator add the item(s) (and maybe set some attributes)
            $decorator($this, $item);
            // Grab the newest item
            $newestItem = end($this->children);
            // If there is an item, add hydrate it
            if ($newestItem) {
                // Grab the newest itemlist
                $newestItemList = $newestItem->getChildren();
                // Get the id of the item
                $parentId = is_object($item) ? $item->{$idField} : $item[$idField];
                // Hydrate the children
                $newestItemList->hydrate($items, $decorator, $idField, $parentIdField, $parentId);
            }
        }
        return $this;
    }