示例#1
0
 /**
  * Alias for getCollection
  *
  * @return \Illuminate\Support\Collection 
  * @static 
  */
 public static function all()
 {
     return \Lavary\Menu\Menu::all();
 }
 /**
  * Build a menu and return the menu object
  *
  * This function only supports 2 levels of children.
  *
  * @param MenuModel $menuModel
  * @return LavaryBuilder
  */
 public function renderToObject(MenuModel $menuModel)
 {
     // Get the menu name, which will be the name of the variable shared
     // to all of the views.
     $menu_name = Str::studly($menuModel->name);
     // Create the first item in the menu.  Don't use the facade because
     // it may not work if the alias hasn't been created, and I don't document
     // creating the alias in my own README because the alias name conflicts
     // with one of my own class names.
     $lavaryMenu = new LavaryMenu();
     $lavaryMenu->make($menu_name, function ($menu) use($menuModel) {
         $menu_data = $this->getMenuData($menuModel);
         // The first menu item may be a header.
         /** @var LavaryBuilder $menu */
         /** @var MenuItem $menuItem */
         if (empty($menu_data['url']) && empty($menu_data['route'])) {
             $menuItem = $menu->raw($menuModel->name, $menu_data);
         } else {
             $menuItem = $menu->add($menuModel->name, $menu_data);
             // Append and prepend
             $menuItem->prepend('<i class="fa fa-link""></i> <span>')->append('</span>');
         }
         // Create all of the first level children as siblings of the header.
         /** @var MenuModel $descendant */
         foreach ($menuModel->getImmediateDescendants() as $descendant) {
             $descendantItem = $this->renderChildNode($descendant, $menu);
             // Create all of the second level children
             foreach ($descendant->getImmediateDescendants() as $grandchild) {
                 $this->renderChildNode($grandchild, $descendantItem);
             }
         }
     });
     // LavaryMenu::make puts a LavaryBuilder item in the collection,
     /** @var LavaryBuilder $builder */
     $builder = $lavaryMenu->get($menu_name);
     return $builder;
 }