在同一模型上雄辩的亲子关系

Kir*_* LM 18 php laravel eloquent laravel-5 laravel-5.1

我有一个模型CourseModule,每个项目都与相同的模型相关.

数据库结构:

在此输入图像描述

模型中的关系:

    public function parent()
    {
        return $this->belongsTo('App\CourseModule','parent_id')->where('parent_id',0);
    }

    public function children()
    {
        return $this->hasMany('App\CourseModule','parent_id');
    }
Run Code Online (Sandbox Code Playgroud)

我尝试了以下内容,但它只返回一个级别的关系.

尝试:

CourseModule::with('children')->get();
Run Code Online (Sandbox Code Playgroud)

我正在尝试创建一个类似于以下的json输出,

预期产出:

[
  {
    "id": "1",
    "parent_id": "0",
    "course_id": "2",
    "name": "Parent",
    "description": "first parent",
    "order_id": "1",
    "created_at": "-0001-11-30 00:00:00",
    "updated_at": "-0001-11-30 00:00:00",
    "children": [
      {
        "id": "2",
        "parent_id": "1",
        "course_id": "2",
        "name": "Child 1",
        "description": "child of parent",
        "order_id": "2",
        "created_at": "-0001-11-30 00:00:00",
        "updated_at": "-0001-11-30 00:00:00",
        "children": [
          {
            "id": "3",
            "parent_id": "2",
            "course_id": "2",
            "name": "Child2",
            "description": "child of child1",
            "order_id": "2",
            "created_at": "-0001-11-30 00:00:00",
            "updated_at": "-0001-11-30 00:00:00",
            "children": [
              {
                "id": "4",
                "parent_id": "3",
                "course_id": "2",
                "name": "Child 3",
                "description": "child of child 2",
                "order_id": "2",
                "created_at": "-0001-11-30 00:00:00",
                "updated_at": "-0001-11-30 00:00:00",
                "children": []
              }
            ]
          }
        ]
      }
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

我不明白如何获取内部子对象.

小智 15

您应该with('children')在子关系和with('parent')父关系中使用.

为了使您的代码递归:

public function parent()
{
    return $this->belongsTo('App\CourseModule','parent_id')->where('parent_id',0)->with('parent');
}

public function children()
{
    return $this->hasMany('App\CourseModule','parent_id')->with('children');
}
Run Code Online (Sandbox Code Playgroud)

注意:确保您的代码具有某些或其他退出条件,否则它将以永无止境的循环结束.


lag*_*box 8

如果您有这样的未知深度,则必须以递归方式获取子项.

另一种选择是使用嵌套集模型而不是邻接列表模型.baum/baum对于嵌套集,您可以使用类似Laravel的包.

"嵌套集是实现有序树的智能方法,允许快速,非递归查询." - https://github.com/etrepat/baum

使用此包,您getDescendants可以获得所有子项和嵌套子项以及toHierarchy获取完整树层次结构的方法.

维基百科 - 嵌套集模型

Baum - Laravel的Eloquent ORM的嵌套集模式

在MySQL中管理分层数据


Sag*_*ara 5

是可以帮助您的答案

我认为您必须递归执行以检索整棵树:

$data = CourseModule::with('child_rec');
Run Code Online (Sandbox Code Playgroud)

递归函数

这可能会根据您的要求为您提供帮助,

public function child()
{
   return $this->hasMany('App\CourseModule', 'parent');
}
public function children_rec()
{
   return $this->child()->with('children_rec');
   // which is equivalent to:
   // return $this->hasMany('App\CourseModule', 'parent')->with('children_rec);
}
// parent
public function parent()
{
   return $this->belongsTo('App\CourseModule','parent');
}

// all ascendants
public function parent_rec()
{
   return $this->parent()->with('parent_rec');
}
Run Code Online (Sandbox Code Playgroud)