samedi 29 octobre 2016

Menu in laravel 5 Multiple levels?

Model:

class Menu extends Model
{
    protected $table = 'menus';

    public function sub_menu(){
        return $this->hasMany('App\Menu', 'parent_id');
    }

}

Controller:

class MenuController extends Controller
{

    public function index()
    {
        $menus = Menu::where('parent_id', 0)->get();
        return view('admin.menus.index',compact('menus'));
    }
}

View:

<ul>
    @foreach($menus as $menu)
    <li>
    @if($menu->sub_menu->count() > 0)
       <ul>
       @foreach($menu->sub_menu as $sub)
           <li</li>
       @endforeach
       </ul>
   @endif
   </li>
   @endforeach
</ul>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire