MLM网站的PHP递归树视图

I have tried to follow some online sites about how to do a Treeview in PHP. I have it working but I have a ton of MySQL Calls to make it work. I am newer to PHP and I know there has to be a better way of doing it. Any help would be wonderful.

$childid = '';

        $parent = DB::table('ezygold_users')->join('ezygold_usersplan', 'ezygold_users.id', '=', 'ezygold_usersplan.idusrpp')->where('id', '=', $id->users_id)->select('id', 'fullname', 'idref', 'idspr')->get();

        foreach ($parent as $parentout) {
            echo $parentout->fullname;
        }

        echo '<ol>';
        foreach (DB::table('ezygold_users')->join('ezygold_usersplan', 'ezygold_users.id', '=', 'ezygold_usersplan.idusrpp')->where('idref', '=', $parentout->id)->select('id', 'fullname', 'idref', 'idspr')->get() as $child) {
            echo '<li>' . $child->fullname . '</li>';
            $childid = $child->id;

            echo '<ol>';
            foreach (DB::table('ezygold_users')->join('ezygold_usersplan', 'ezygold_users.id', '=', 'ezygold_usersplan.idusrpp')->where('idref', '=', $childid)->select('id', 'fullname', 'idref', 'idspr')->get() as $child) {
                echo '<li>' . $child->fullname . '</li>';
                $childid = $child->id;

                echo '<ol>';
                foreach (DB::table('ezygold_users')->join('ezygold_usersplan', 'ezygold_users.id', '=', 'ezygold_usersplan.idusrpp')->where('idref', '=', $childid)->select('id', 'fullname', 'idref', 'idspr')->get() as $child) {
                    echo '<li>' . $child->fullname . '</li>';
                    $childid = $child->id;

                    echo '<ol>';
                    foreach (DB::table('ezygold_users')->join('ezygold_usersplan', 'ezygold_users.id', '=', 'ezygold_usersplan.idusrpp')->where('idref', '=', $childid)->select('id', 'fullname', 'idref', 'idspr')->get() as $child) {
                        echo '<li>' . $child->fullname . '</li>';
                        $childid = $child->id;
                    }
                    echo '</ol>';
                }
                echo '</ol>';
            }
            echo '</ol>';
        }
        echo '</ol>';

EDIT:

I have it down to this but the return takes about 30 to 45 secs. Can you see why?

function getTree($id)
{
    $child = DB::table('ezygold_users')->join('ezygold_usersplan', 'ezygold_users.id', '=', 'ezygold_usersplan.idusrpp')->select('id', 'fullname', 'idref', 'idspr', 'idusrpp')->get();
        echo '<ul>';
        foreach ($child as $childout) {
        $rollup = DB::table('ezygold_users')->where('id', '=',$childout->idspr)->first();
            if ($childout->idref == $id) {
                echo '<li>' . $childout->fullname . '<br>' . '( ' . $rollup->fullname . ' )</li>';
                $this->getTree($childout->id);
            }

        }

    echo '</ul>';
    return;