How to Add Active Class Dynamically in Laravel 11

Websolutionstuff | Jul-08-2024 | Categories : Laravel Bootstrap

Hello, laravel web developers! In this article, we'll see how to add an active class dynamically in laravel 11. Here, we'll learn about adding an active class on the active menu in laravel 11. You can add an active class on an active URL in laravel 11.

Add active class to a navigation menu based on URL.

Laravel 11 Active Menu Example

laravel active menu

 

Step 1: Add Routes

In this step, we'll define the routes into the web.php file.

routes/web.php

Route::get('/home', 'HomeController@index')->name('home');
Route::get('/about-us', 'HomeController@aboutUS')->name('aboutUS');
Route::get('/contact-us', 'HomeController@contactUS')->name('contactUS');
Route::get('/pricing', 'HomeController@pricing')->name('pricing');

 

Step 2: Add Active Class Dynamically

Now, we'll add an active class dynamically based on the URL.

<ul>
    <li class="{{ request()->is('home*') ? 'active' : '' }}">
        <a href="{{ route('home') }}">Home</a>
    </li>
    <li class="{{ request()->is('about-us') ? 'active' : '' }}">
        <a href="{{ route('aboutUS') }}">About US</a>
    </li>
    <li class="{{ request()->is('contact-us') ? 'active' : '' }}">
        <a href="{{ route('contactUS') }}">Contact US</a>
    </li>
    <li class="{{ request()->is('pricing') ? 'active' : '' }}">
        <a href="{{ route('pricing') }}">Pricing</a>
    </li>
</ul>

 

Step 3: Add Active Class CSS

Now, add CSS for the active class.

.active {
    background-color: #ddd;
    color: #fff;
}

 


You might also like:

Recommended Post
Featured Post
How To Image Upload Using Ajax In Laravel 9
How To Image Upload Using Ajax...

In this article, we will see how to image upload using ajax in laravel 9. Here, we will learn about image upload in...

Read More

Feb-07-2023

How To Store Multiple Checkbox Value In Database Using Laravel
How To Store Multiple Checkbox...

In this post we will see how to store multiple checkbox value in database using laravel. Whenever you want to save multi...

Read More

May-14-2021

Top 12 Tips To Improve React JS Performance In 2023
Top 12 Tips To Improve React J...

In the dynamic world of web development, staying ahead of the curve is essential, and in 2023, React JS continues to be...

Read More

Aug-28-2023

How to Install and Configure Elasticsearch on Ubuntu
How to Install and Configure E...

Hey there! Today, I'm going to walk you through the process of installing and setting up Elasticsearch on your Ubunt...

Read More

Jan-08-2024