0

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?

Soumya Rauth
  • 1,163
  • 5
  • 16
  • 32

1 Answers1

0

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');
}
Yat23
  • 181
  • 4