dongwei6700 2017-03-23 10:26
浏览 30
已采纳

Laravel Eloquent多层次自我加入

How to do multi level self join in laravel 5.4
i have table like this.

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 

Now i need:
# if i call id 1 it will return full tree under it.
# if i call it empty it will return full tree
# i did this

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

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

its giving me single brunch but i need full of them.



some one plz help.

  • 写回答

1条回答 默认 最新

  • doumen5087 2017-08-08 07:08
    关注
    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 pyqt5读取ui文件报错
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?