MB34
MB34

Reputation: 4424

Laravel Menu issue

I have the code below to read my menu items from a model, loaded from a DB. I need to set them active based on the current URL. This code only works for the root '/' it doesn't work for others, even though if I echo out the values, url($menu->link) and Request::url() they are the same.

@foreach($menus as $menu)
    <li>
        <a href="{{url($menu->link) }}" @if(Request::is($menu->link))  class="active" @endif>
            <span class="glyphicon {{ $menu->icon }}"></span>
                {{ $menu->title }}
        </a>
    </li>
@endforeach

What am I doing wrong?

Upvotes: 1

Views: 104

Answers (1)

Prince Lionel N&#39;zi
Prince Lionel N&#39;zi

Reputation: 2588

I guess $menu->link will return a string that look like posts, /posts/edit, etc...

If that is the case, you can do

@foreach($menus as $menu)
    <li>
        <a href="{{url($menu->link) }}" @if(url()->current() == url($menu->link))  class="active" @endif>
            <span class="glyphicon {{ $menu->icon }}"></span>
                {{ $menu->title }}
        </a>
    </li>
@endforeach

Upvotes: 1

Related Questions