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.
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;
}