getting a particular data from the parent node

204 Views Asked by At

I am new in neoeloquent. I have created some node which has some child nodes. But I can't figure out how do I get the data of the parent node from the child node?

1

There are 1 best solutions below

0
On

This is a recursive relationship. In the Model, you just need to setup relationships as follows, assuming your keys are setup correctly. Then you can load the relationship using the with('parent) or load('parent) methods in your query.

Relationships:

//Node.php

public function children(){
  return $this->hasMany('App\Node', 'parent_id', 'id');
}

public function parent(){
  return $this->belongsTo('App\Node', 'parent_id', 'id');
}