Eloquent Parent-Child relationship on same model

前端 未结 6 1478
醉酒成梦
醉酒成梦 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:17

    You can always create your own recursive function, in my case I do as the code as follow.

    first();
    
            if (!$lowestLevel) {
                return $tree;
            }
    
            $tree[] = $lowestLevel->toArray();
    
            if ($lowestLevel->parent_id !== 0) {
                $tree = Location::getTree($lowestLevel->parent_id, $tree);
            }
    
            return $tree;
    
        }
    
    }
    

提交回复
热议问题