Laravel Dynamic Active Class Menu

Today, We want to share with you Laravel Dynamic Active Class Menu.In this post we will show you Laravel 5.7 Add active class to main menu, hear for Getting a menu item to be active dynamically using Laravel 5.7 we will give you demo and example for implement.In this post, we will learn about Dynamically Add Active Classes To Nav With PHP Laravel 5.7 and Bootstrap with an example.

Laravel Dynamic Active Class Menu

There are the Following The simple About Laravel Dynamic Active Class Menu Full Information With Example and source code.

As I will cover this Post with live Working example to develop how to add active class in menu using Laravel 5.7, so the some laravel 5.7 active menu item for this example is following below.

Also Read :  Multiple Database Connections using Laravel

Laravel Dynamic Active menu

 <ul class="nav nav-pills pull-right">
    <li class="{{ Request::is('/') ? 'active' : '' }}">
        <a href="{{ url('/') }}">Home</a>
    </li>
    <li class="{{ Request::is('contact') ? 'active' : '' }}">
        <a href="{{ url('contact') }}">Contact Us</a>
    </li>
    <li class="{{ Request::is('auth/signin') ? 'active' : '' }}">
        <a href="{{ url('auth/signin') }}">Login</a>
    </li>
</ul>

Laravel 5.5 active menu item using request

<ul class="nav nav-second-level">
    <li class="{{ Request::segment(1) === 'examples' ? 'active' : null }}">
        <a href="{{ url('examples' )}}" ></i> examples</a>
    </li>
    <li class="{{ Request::segment(1) === 'teams' ? 'active' : null }}">
        <a href="{{url('teams')}}"> teams</a>
    </li>
    <li class="{{ Request::segment(1) === 'members' ? 'active' : null }}">
        <a href="{{url('members')}}"> members</a>
    </li>                     
</ul>

Laravel active menu item for url included parameters

<li class="{{ request()->is('sites/*/edit') ? 'active' : '' }}"
//or
<a href="#" class="nav-link {{ request()->is('employee*') ? 'active' : '' }}">employee</a>

Handling active menu item in Laravel 5

Put this in your helper file:

Also Read :  Login with LinkedIn using JavaScript SDK

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

Usage:

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

Laravel Nave Active Menu Multi-level:

<li class="{{ set_active(['member/items', Request::is('member/auction/*'), 'member/templates']) }}">
    <ul>
        <li class = "{{ set_active('member/item') }}">
            <a href="{{ url('/member/item') }}">All</a>
        </li>
        <li class = "{{ set_active('member/item/create') }}">
            <a href="{{ URL::to('/member/item/create') }}">Add New</a>
        </li>
        <li class = "{{ set_active('member/templates') }}">
            <a href="{{ URL::to('/member/templates') }}">templates</a>
        </li>
    </ul>
</li>

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel Dynamic Active Class Menu.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Gondaliya Jaydeep

Hi, I’m Jaydeep Gondaliya, PHP developer.Pakainfo.com is the most popular Programming & Web Development blog. Our mission is to provide the best online resources on programming and web development.Any visitors of this site are free to browse our tutorials, live demos, Examples and download scripts.If you have a project that you want to get started, launch quickly, contact me. Email : [email protected]