Laravel 6 Set Active Navigation Menu Example

Today, We want to share with you Laravel 6 Set Active Navigation Menu.In this post we will show you Dynamically add active menu item in Bootstrap using Laravel 6, hear for Laravel active menu item for url included parameters we will give you demo and example for implement.In this post, we will learn about Apply Dynamic Active Class on Current Menu Item Laravel with an example.

Laravel 6 Set Active Navigation Menu

There are the Following The simple About Laravel 6 Set an Active Menu Link From Scratch Full Information With Example and source code.

As I will cover this Post with live Working example to develop How to add dynamic Active class on selected page using Laravel 6, so the how to set bootstrap navbar class active in Laravel 6 is used for this example is following below.

How to Make Menu Item Active by URL/Route using Laravel?

<li>
    <a href="{{ route('products.categories.index') }}">
        <i class="fa fa-flag"></i> <span>Category</span>
    </a>
</li>
<li class="active">
    <a href="{{ route('products.categories.index') }}">
        <i class="fa fa-file"></i> <span>Menu</span>
    </a>
</li>
<li>
    <a href="{{ route('products.categories.index') }}">
        <i class="fa fa-edit"></i> <span>Types</span>
    </a>
</li>

we’re interested in is class=”active”.

<li class="{{ ($some_condition_if_active) ? 'active' : '' }}">  

Example 1: Exact URL in Laravel

use helper function $request->is().

<li class="{{ (request()->is('products/categories')) ? 'active' : '' }}">  

Example 2:Starting with the URL

URL like /products/categories, /post/categories/edit etc.

<li class="{{ (request()->is('products/categories*')) ? 'active' : '' }}">  

Example 3: Matching URL Segment in Laravel

laravel get URL segments

<li class="{{ (request()->segment(2) == 'categories') ? 'active' : '' }}">  

URL like /products/categories, /post/categories/edit etc.

Example 4: Matching Route by Name

routes/web.php

Route::middleware(['auth'])->prefix('products')->name('products.')->group(function() {
    Route::resource('categories', 'Admin\CategoryController');
});

Accessing The Current Route

Laravel 6 current, currentRouteName, and currentRouteAction methods

$route = Route::current();
dump($route);

$name = Route::currentRouteName();
dump($name);

$action = Route::currentRouteAction();
dump($action);

we use Route::getCurrentName() method.

<li class="{{ (strpos(Route::currentRouteName(), 'products.categories') == 0) ? 'active' : '' }}">  

Web Programming Tutorials Example with Demo

Read :

Also Read This 👉   html code to send email on button click

Summary

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

I hope you get an idea about Laravel 6 active menu item using request.
I would like to have feedback on my infinityknow.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.