0

我是neoeloquent的新手。我创建了一些具有一些子节点的节点。但是我不知道如何从子节点获取父节点的数据?

4

1 回答 1

0

这是一种递归关系。在模型中,您只需要设置如下关系,假设您的密钥设置正确。with('parent)然后,您可以使用查询中的orload('parent)方法加载关系。

关系:

//Node.php

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

public function parent(){
  return $this->belongsTo('App\Node', 'parent_id', 'id');
}
于 2017-07-19T11:10:25.817 回答