src/Gzero/EloquentTree/Model/Tree.php
public function buildTree(Collection $nodes, $strict = true)
    {
        $refs  = []; // Reference table to store records in the construction of the tree
        $count = 0;
        $roots = new Collection();
        foreach ($nodes as &$node) {
            /* @var Tree $node */
            $node->initChildrenRelation(); // We need to init relation to avoid LAZY LOADING in future
            $refs[$node->getKey()] = &$node; // Adding to ref table (we identify after the id)
            if ($count === 0) {
                $roots->add($node);
                $count++;
            } else {
                if ($this->siblingOfRoot($node, $roots)) { // We use this condition as a factor in building subtrees
                    $roots->add($node);
                } else { // This is not a root, so add them to the parent
                    $index = $node->{$this->getTreeColumn('parent')};
                    if (!empty($refs[$index])) { // We should already have parent for our node added to refs array
                        $refs[$index]->addChildToCollection($node);
                    } else {
                        if ($strict) { // We don't want to ignore orphan nodes
                            throw new MissingParentException();
                        }
                    }
                }
            }
        }

        if (!empty($roots)) {
            if (count($roots) > 1) {
                return $roots;
            } else {
                return $roots->first();
            }
        } else {
            return false;
        }
    }
public function buildTree(Collection $nodes, $strict = true)
    {
        $refs  = []; // Reference table to store records in the construction of the tree
        $count = 0;
        $roots = new Collection();
        foreach ($nodes as &$node) {
            /* @var Tree $node */
            $node->initChildrenRelation(); // We need to init relation to avoid LAZY LOADING in future
            $refs[$node->getKey()] = &$node; // Adding to ref table (we identify after the id)
            if ($count === 0) {
                $roots->add($node);
                $count++;
            } else {
                if ($this->siblingOfRoot($node, $roots)) { // We use this condition as a factor in building subtrees
                    $roots->add($node);
                } else { // This is not a root, so add them to the parent
                    $index = $node->{$this->getTreeColumn('parent')};
                    if (!empty($refs[$index])) { // We should already have parent for our node added to refs array
                        $refs[$index]->addChildToCollection($node);
                    } else {
                        if ($strict) { // We don't want to ignore orphan nodes
                            throw new MissingParentException();
                        }
                    }
                }
            }
        }

        if (!empty($roots)) {
            if (count($roots) > 1) {
                return $roots;
            } else {
                return $roots->first();
            }
        } else {
            return false;
        }
    }
public function buildTree ( Collection $ nodes , $ strict = true ) { $ refs = [ ] ; // Reference table to store records in the construction of the tree $ count = 0 ; $ roots = new Collection ( ) ; foreach ( $ nodes as & $ node ) { /* @var Tree $node */ $ node -> initChildrenRelation ( ) ; // We need to init relation to avoid LAZY LOADING in future $ refs [ $ node -> getKey ( ) ] = & $ node ; // Adding to ref table (we identify after the id) if ( $ count === 0 ) { $ roots -> add ( $ node ) ; $ count ++ ; } else { if ( $ this -> siblingOfRoot ( $ node , $ roots ) ) { // We use this condition as a factor in building subtrees $ roots -> add ( $ node ) ; } else { // This is not a root, so add them to the parent $ index = $ node -> { $ this -> getTreeColumn ( 'parent' ) } ; if ( ! empty ( $ refs [ $ index ] ) ) { // We should already have parent for our node added to refs array $ refs [ $ index ] -> addChildToCollection ( $ node ) ; } else { if ( $ strict ) { // We don't want to ignore orphan nodes throw new MissingParentException ( ) ; } } } } } if ( ! empty ( $ roots ) ) { if ( count ( $ roots ) > 1 ) { return $ roots ; } else { return $ roots -> first ( ) ; } } else { return false ; } }
Rebuilds trees from passed nodes

@param Collection $nodes  Nodes from which we are build tree
@param bool       $strict If we want to make sure that there are no orphan nodes

@return static Root node
@throws MissingParentException
Rebuilds trees from passed nodes