在laravel

时间:2017-03-20 13:53:53

标签: laravel laravel-5

我正在尝试在laravel 5.4中创建一个菜单。我遇到的问题是因为我的代码,如果我的菜单项有一个seo附加到它然后我的链接是 在li标签内,但是因为这个,如果我的菜单项没有附加到它的seo我只是有一个不在li标签内的链接。我不是 确定如何去做,以便我可以获得li标签内的链接,无论是否有seo。

因为它位于我的萤火虫中,这就是我的菜单看起来的样子

<nav class="navbar navbar-default">
    <div class="container-fluid">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main_menu" aria-expanded="false">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
      </div>

      <div class="collapse navbar-collapse menubar" id="main_menu">
        <ul class="nav navbar-nav" id="menu">
          <li class="parent ">
            <a href="http://localhost/writers-circle/public/home.html">home</a>
          </li>
          <a href="http://localhost/writers-circle/public/3">stories</a> <!-- HERE IS WHERE THE PROBLEM IS -->
        </ul>
      </div>
    </div>
</nav>

menu.blade.php

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main_menu" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>

    <div class="collapse navbar-collapse menubar" id="main_menu">
      <ul class="nav navbar-nav" id="menu">
        @foreach($menus_child as $grandfather)
          @foreach($grandfather->seo as $seo)
            <li class="parent {!! (current_page($seo->url)) ? 'current' : '' !!}">
          @endforeach
            {!! Html::link(getSeoLink($grandfather->id), $grandfather->title) !!}
            </li>
        @endforeach
      </ul>
    </div>
  </div>
</nav>

我希望我的问题有道理。

1 个答案:

答案 0 :(得分:0)

我得到了!!!问题实际上在我的helpers.php文件中。我终于设法让它发挥作用。

这就是我在helpers.php文件中所做的

function menu_active($id){

        $test_id = $id;
         $testing = App\Modules\Menus\Models\Menu::with('seo')->where('id', $test_id)->first();
         $seo_url = $testing->seo;

         foreach($seo_url as $t){
            $url = $t['url'];
            if(is_array($url)){
                return in_array(Request::path(), $url) ? 'current' : '';
            }

             return Request::path() == $url ? 'current' : '';
         }

        if(is_array($id)){
            return in_array(Request::path(), $id) ? 'current' : '';
        }

        return Request::path() == $id ? 'current' : '';
    }

然后我在menu.blade.php中更改了我的ul,看起来像这样

<ul class="nav navbar-nav" id="menu">
        @foreach($menus_child as $grandfather)
          <li class="parent {!! menu_active($grandfather->id) !!}">
            {!! Html::link(getSeoLink($grandfather->id), $grandfather->title) !!}
          </li>
        @endforeach
      </ul>