hasMany(\App\Models\Causal::class,'parent_id','id') ; } public function getTree() { $str = ''; if ($this->parent_id != null) { $a = $this->recursiveName($this->parent_id, array($this->name)); $a = array_reverse($a); $str = implode(" - ", $a); } else { $str = $this->name; } return $str; } public function recursiveName($parent_id, $array) { $x = \App\Models\Causal::findOrFail($parent_id); $array[] = $x->name; if ($x->parent_id != null) { return $this->recursiveName($x->parent_id, $array); } else { return $array; } } }