同一模型上雄辩的父子关系 [英] Eloquent Parent-Child relationship on same model

查看:31
本文介绍了同一模型上雄辩的父子关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

数据库结构:

模型中的关系:

 公共函数 parent(){return $this->belongsTo('AppCourseModule','parent_id')->where('parent_id',0);}公共函数 children(){返回 $this->hasMany('AppCourseModule','parent_id');}

我尝试了以下方法,但它只返回一个关系级别.

尝试过:

CourseModule::with('children')->get();

我正在尝试创建如下所示的 json 输出,

预期输出:

<预><代码>[{id":1",parent_id":0",course_id":2",姓名":父母",描述":第一父母",order_id":1",created_at":-0001-11-30 00:00:00",updated_at":-0001-11-30 00:00:00",儿童":[{id":2",parent_id":1",course_id":2",姓名":孩子 1",描述":父母的孩子",order_id":2",created_at":-0001-11-30 00:00:00",updated_at":-0001-11-30 00:00:00",儿童":[{id":3",parent_id":2",course_id":2",名称":Child2",描述":child1 的孩子",order_id":2",created_at":-0001-11-30 00:00:00",updated_at":-0001-11-30 00:00:00",儿童":[{id":4",parent_id":3",course_id":2",名称":儿童 3",描述":孩子 2 的孩子",order_id":2",created_at":-0001-11-30 00:00:00",updated_at":-0001-11-30 00:00:00",儿童":[]}]}]}]}]

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

解决方案

如果您有这样的未知深度,您将不得不递归获取子项.

另一种选择是使用嵌套集模型而不是邻接表模型.您可以使用 Laravel 的 baum/baum 包来实现嵌套集.

<块引用>

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

使用这个包,您可以使用诸如 getDescendants 之类的方法来获取所有子项和嵌套子项,以及 toHierarchy 来获取完整的树层次结构.

维基百科 - 嵌套集模型

Baum - Laravel Eloquent ORM 的嵌套集模式

在 MySQL 中管理分层数据

I have a model CourseModule, and each of the items are related to the same model.

Database Structure:

Relation in Model:

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

    public function children()
    {
        return $this->hasMany('AppCourseModule','parent_id');
    }

I tried the following, but it returns only a single level of relation.

Tried:

CourseModule::with('children')->get();

I'm trying to create a json output like the following,

Expected Output:

[
  {
    "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": []
              }
            ]
          }
        ]
      }
    ]
  }
]

I don't understand how to get the inner child objects.

解决方案

You would have to recursively get the children if you have an unknown depth like that.

Another option is to use the nested sets model instead of the adjacency list model. You can use something like baum/baum package for Laravel for nested sets.

"A nested set is a smart way to implement an ordered tree that allows for fast, non-recursive queries." - https://github.com/etrepat/baum

With this package you have methods like getDescendants to get all children and nested children and toHierarchy to get a complete tree hierarchy.

Wikipedia - Nested Set Model

Baum - Nested Set pattern for Laravel's Eloquent ORM

Managing Hierarchical Data in MySQL

这篇关于同一模型上雄辩的父子关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆