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>



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2fqQhTM
via IFTTT

Aucun commentaire:

Enregistrer un commentaire