Eloquent Parent-Child relationship on same model

前端 未结 6 1482
醉酒成梦
醉酒成梦 2020-12-03 07:42

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

Database Structure:

Relation in Model:



        
6条回答
  •  温柔的废话
    2020-12-03 08:09

    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

提交回复
热议问题