PHP Laravel Dynamically Add Active Navigation Class Example

Today, We want to share with you PHP Laravel Dynamically Add Active Navigation Class Example.In this post we will show you Laravel Add Active Navigation Class Based on URL, hear for Dynamically add active menu item in Bootstrap using Laravel we will give you demo and example for implement.In this post, we will learn about How add active state to the navigation menu with Laravel with an example.

PHP Laravel Dynamically Add Active Navigation Class Example

There are the Following The simple About PHP Laravel Dynamically Add Active Navigation Class Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel Add Active Navigation Class Based on URL, so the some major files and Directory structures for this example is following below.

  • Laravel Dynamic active menu
  • Add active class to menu with Laravel

Example 1 : Laravel Dynamically Add active class to Navigation Menu

layouts/layouts.blade.php

<h3>Laravel 5.7 Dynamic active menu Example</h3>
<div class="subnavbar">
    <div class="subnavbar-inner">
        <div class="container">
            <ul class="mainnav">
                @guest
                <li class="{{ Request::is('activeproducts') ? 'active' : '' }}"><a href="{{ url('activeproducts') }}"><i class="icon-thumbs-up-alt"></i><span>Active Products <span class="badge">{{$totalactiveproducts}}</span></span></a> </li>
                <li class="{{ Request::is('completeproducts') ? 'active' : '' }}"><a href="{{ url('completeproducts') }}"><i class="icon-folder-close"></i><span>Completed Products <span class="badge">{{$totalcompleteproducts}}</span></span></a> </li>
                @else
                <li class="{{ Request::is('home') ? 'active' : '' }}"><a href="{{ url('home') }}"><i class="icon-dashboard"></i><span>Dashboard</span> </a> </li>
                <li class="{{ Request::is('createproduct') ? 'active' : '' }}"><a href="{{ url('createproduct') }}"><i class="icon-thumbs-up-alt"></i><span>Active Products <span class="badge">{{ $activeproducts }}</span></span> </a> </li>
                <li class="{{ Request::is('completeproduct') ? 'active' : '' }}"><a href="{{ url('completeproduct') }}"><i class="icon-folder-close"></i><span>Completed Products <span class="badge">{{$completeproducts}}</span></span> </a> </li>
                <li class="{{ Request::is('users') ? 'active' : '' }}"><a href="{{ url('users') }}"><i class="icon-user"></i><span>Users</span> </a> </li>
               <li class="dropdown {{ Request::is('teamlist') || Request::is('statuslist') || Request::is('categorieslist') || Request::is('configuration') || Request::is('roles') || Request::is('usersoftware') ? 'active' : '' }}"><a href=";" class="dropdown-toggle"> <i class="icon-wrench"></i><span>Settings</span> <b class="caret"></b></a>
                <ul class="dropdown-menu">
                    <li><a href="{{ url('teamlist') }}">Teams</a></li>
                    <li><a href="{{ url('categorieslist') }}">Software</a></li>
                    <li><a href="{{ url('usersoftware') }}">User Softwares</a></li>
                    <li><a href="{{ url('statuslist') }}">Status</a></li>
                    <li><a href="{{ url('roles') }}">Roles</a></li>
                    <li><a href="{{ url('configuration') }}">configuration</a></li>
                </ul>
                </li>
                @endguest
            </ul>
        </div>
        <!-- /container -->
    </div>
    <!-- /subnavbar-inner -->
</div>

Example 2 : Dynamically add active menu Item using Laravel

<h3>How TO - Add Active Class to Current Element in Laravel 5.6,5.7*</h3>
<p>Simple Live Example of the PHP Laravel Dynamically Add Active Navigation Class Example</p>
<div class="subnavbar">
    <div class="subnavbar-inner">
        <div class="container">
            <ul class="nav nav-tabs" role="tablist">
                @foreach ($nav_menu_names as $menu_name)
                    @if ($menu_name == 'products' && Request::segment(3) == null)
                        <li class="nav-menu_name">
                            {{ link_to(URL::route('users.show', $user->id), str_replace('_', ' ', $menu_name), array('class' => 'nav-link active', 'role' => 'tab')) }}</li>
                    @elseif ($menu_name == 'products')
                        <li class="nav-menu_name">
                            {{ link_to(URL::route('users.show', $user->id), str_replace('_', ' ', $menu_name), array('class' => 'nav-link', 'role' => 'tab')) }}</li>
                    @elseif ($menu_name == camel_case(Request::segment(3)))
                        <li class="nav-menu_name">{{ link_to(URL::route('users.' . $menu_name . '.index', $user->id), str_replace('_', ' ', $menu_name), array('class' => 'nav-link active', 'role' => 'tab' )) }}</li>
                    @else
                        <li class="nav-menu_name">{{ link_to(URL::route('users.' . $menu_name . '.index', +$user->id), str_replace('_', ' ', $menu_name), array('class' => 'nav-link', 'role' => 'tab')) }}</li>
                    @endif
                @endforeach
            </ul>
        </div>
    </div>
</div>

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  File Type validation using Javascript

Summary

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

I hope you get an idea about PHP Laravel Dynamically Add Active Navigation Class Example.
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.