Eloquent Parent-Child relationship on same model

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

    Model Function:

    public function Children()
    { 
        return $this->hasMany(self::class, 'Parent', 'Id')->with('Children');
    } 
    

    Controller Function:

    Menu::with("Children")->where(["Parent" => 0])->get(); 
    

提交回复
热议问题