2017-03-23 4 views
0

Wie zu tun, Multi-Level-Selbst beitreten in Laravel 5.4
Ich habe Tabelle wie folgt.Laravel Eloquent Multi Level Selbst Join

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 

Jetzt brauche ich:
# if i id 1 Aufruf wird es voll Baum unter ihm zurückzukehren.
# wenn ich es leer nennen es ich voll Baum
# return this

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

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

its me Einzel Brunch geben, aber ich brauche voll davon.



jemand eine PLZ Hilfe.

+1

Endlich kann ich es tun. Ich habe das getChild in rekursiver Funktion aufgerufen. – Azad

+0

wie Sie lösen, können Sie mir Ihren Code sehen? –

Antwort

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