function build_tree($comments) { if ($comments) { $result = "<ul>\n"; foreach ($comments as $comment) { $result .= "<li>"; $result .= $comment->id; $result .= "</li>\n"; // $count = Comment::findorFail(2)->childrens; $children = Comment::findorFail($comment->id)->childrens; if (isset($children)) { $result .= build_tree($children); } $result .= "</li>\n"; } $result .= "</ul>"; return $result; } }