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 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败