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; }