Reputation: 1201
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?
Upvotes: 0
Views: 195
Reputation: 171
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');
}
Upvotes: 0