I have a model CourseModule
, and each of the items are related to the same model.
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