doulian8485 2014-08-07 17:10
浏览 181
已采纳

动态菜单laravel

I am starting with laravel, and made an integration with bootstrap. I want to make a menu bar to access to the information by year, the menu is made I this way to the view

<li class="dropdown-submenu">
    <a tabindex="-1" href="#">2007</a>
    <ul class="dropdown-menu">
        <li><a tabindex="-1" href="<?php echo url('/convenios/2007/registrar', $parameters = array(), $secure = null); ?>">Registrar</a></li>
        <li><a href="#">Consultar</a></li>
    </ul>
</li>

I made this on a view called base.blade.php, there is a better way to make the menus or I made this in the right way?

  • 写回答

1条回答 默认 最新

  • doupacan2098 2016-04-15 21:06
    关注

    menus table

    id - parent_id - title - url - order - created_at - updated_at

    enter image description here

    Make Menu model

    class Menu extends Model
    {
    
      public $timestamps = false;
    
      protected $table = 'menus';
    
      protected $fillable = array('parent_id','title','url','order');
    
      public function parent()
      {
        return $this->belongsTo('App\Menu', 'parent_id');
      }
    
      public function children()
      {
        return $this->hasMany('App\Menu', 'parent_id');
      }
    }
    

    in View

    @foreach(App\Menu::orderBy('order','asc')->get() as $menuItem)
    
      @if( $menuItem->parent_id == 0 )
         <li {{ $menuItem->url ? '' : "class=dropdown" }}>
         <a href="{{ $menuItem->children->isEmpty() ? $menuItem->url : "#" }}"{{ $menuItem->children->isEmpty() ? '' : "class=dropdown-toggle data-toggle=dropdown role=button aria-expanded=false" }}>
            {{ $menuItem->title }}
         </a>
      @endif
    
      @if( ! $menuItem->children->isEmpty() )
        <ul class="dropdown-menu" role="menu">
          @foreach($menuItem->children as $subMenuItem)
              <li><a href="{{ $subMenuItem->url }}">{{ $subMenuItem->title }}</a></li>
          @endforeach
        </ul>
      @endif
      </li> 
    
    @endforeach
    

    you can make controller as you like

    this way i spent all day trying to get this working code, when i finished i decide to search for anybody ask for that issue

    Have fun :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?