navbar not active when go to another route

Viewed 32

So i want to make my navbar post menu still active when i move from show posts route to the single post route.
this is the route: http://127.0.0.1:8000/posts/quia-laboriosam-nam-nostrum-porro
this is the navbar code

<li class="{{ set_active('posts') }}">
    <a href="{{ url('/posts')}}">Blog</a>
</li>

this is the helper

function set_active($route)
{
    if(is_array($route)){
        return in_array(Request::path(), $route) ? 'active-menu' : '';
    }
    return Request::path() == $route ? 'active-menu' : '';
}

how can i make the navbar menu stay active?

1 Answers

You can use the below method to handle navbar active or inactive in laravel,

<li @if(Request::is('posts') || Request::is('singleposts')) class="active" @endif>
    <a href="{{ url('/posts')}}">Blog</a>
</li>
Related