Handling active menu item in Laravel 5

Put this in your helper file:

1
2
3
4
5
6
function set_active( $route ) {
    if( is_array( $route ) ){
        return in_array(Request::path(), $route) ? 'active' : '';
    }
    return Request::path() == $route ? 'active' : '';
}

Usage:

1
2
3
<li class = "{{ set_active('admin/users') }}">
    <a href="{{ url('/admin/users/') }}">Users</a>
</li>

Multi-level:

1
2
3
4
5
6
7
8
9
10
11
12
13
<li class="{{ set_active(['admin/items', Request::is('admin/auction/*'), 'admin/something']) }}">
    <ul>
        <li class = "{{ set_active('admin/item') }}">
            <a href="{{ url('/admin/item') }}">All</a>
        </li>
        <li class = "{{ set_active('admin/item/create') }}">
            <a href="{{ URL::to('/admin/item/create') }}">Add New</a>
        </li>
        <li class = "{{ set_active('admin/something') }}">
            <a href="{{ URL::to('/admin/something') }}">Something</a>
        </li>
    </ul>
</li>


Do you need help with a project? or have a new project in mind that you need help with?

Contact Me