mardi 27 septembre 2016

How do I make one to many relationship to same table in Laravel 5?

I have a table called menus.I want to retrieve menus with its child menus.But only parent menus are being retrieved.Child menus result is an empty array. my table structure is: id | parent_id | name. My Menu model one to many relationship is: public function childMenus() { return $this->hasMany( ‘App\Menu’, ’parent_id’); } public function parentMenus() { return $this->belongsTo(‘App\Menu’, ‘parent_id’); }
My controller Method: public function index() { $menu = new Menu; $parent_menus = $menu->where('parent_id', NULL)->get(); $sub_menus = $menu->childMenus()->get(); return View('admin',compact('$parent_menus’, ‘sub_menus’); } When I dive dump sub_menus dd($sub_menus) it returns an empty array.please anyone help. thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire