app\Category::getParentTreeAttribute PHP Method

getParentTreeAttribute() public method

The tree contains the master parent, and a child attribute that contains next element, up to.
public getParentTreeAttribute ( ) : app\category
return app\category model
    public function getParentTreeAttribute()
    {
        if (!$this->hasParent()) {
            return;
        }
        //family tree (return all category parents)
        $tree = $this->parent;
        $tree->child = null;
        while ($tree->hasParent()) {
            $new = $tree->parent;
            $new->child = $tree;
            $tree = $new;
        }
        return $tree;
    }