0
我是新手。我創建了一些有一些子節點的節點。但我不知道如何從子節點獲取父節點的數據?從父節點獲取特定數據
我是新手。我創建了一些有一些子節點的節點。但我不知道如何從子節點獲取父節點的數據?從父節點獲取特定數據
這是遞歸關係。在模型中,假設您的密鑰設置正確,您只需按如下方式設置關係。然後,您可以在查詢中使用with('parent)
或load('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');
}