Laravel Eloquent 多级自连接

Laravel Eloquent multi level Self Join

如何在 laravel 5.4
中进行多级自连接 我有 table 这样的。

ID    name    ParentId 
1     abc       0       
2     acd       1       
3     ads       1       
4     xyz       2       
5     xxy       2       
6     plm       3       
7     ytr       4       
8     lks       6 

现在我需要:
# 如果我调用 id 1 它将 return 完整的树在它下面。
# 如果我称它为空它将 return 完整的树
#我做到了

public function getParent() {
        return $this->belongsTo(self::class, 'ParentId','id');
    }

    public function getChild(){
        return $this->hasMany(self::class, 'ParentId','id');
    }

它给了我一个早午餐,但我需要吃饱。



请哪位帮忙。

public function  chartLedgre($headId) {
    $mainHead = self::where('id',$headId)->get();
    if(count($mainHead[0]->childs) > 0){
        foreach ($mainHead[0]->childs as $child){
            if($child->chart_type == 'L'){
                $this->data[] = $child->id;
            }else{
                $this->chartLedgre($child->id);
            }
        }
    }else{
        if($mainHead[0]->chart_type == 'L'){
            $this->data[] = $mainHead[0]->id;
        }
    }
    return $this->data;
}