How To Add Active Class To Menu Item In Laravel

  • 07-06-2022
  • 1127
  • Laravel 9
  • Haresh Chauhan

The is() method will check retrive method path from the url and check with whatever you pass string inside this method. The is() is used to collect the current URL from the request and it will match with particular pattern which we specify in this method argument.

Sometimes developers need to select the current page category for the user to know what they are.

Example 1:

Simple request check, If the request comes without a path then it will return true else false.

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {

    \Request::is('/');

    // OUTPUT => true

/*-----------------------------------------*/

    \Request::is('home');

    // OUTPUT => false
});

Example 2:

If the request comes with a home path URL then it will give us a true request else false.

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/home',function(){

    \Request::is('home');
        
    // OUTPUT => true

    \Request::is('contact');

    // OUTPUT => false
});

Example 3:

a the tag inside the class, Whats I what that whenever a request comes I want that category should hover or selected.

So I added a condition inside the tag class if the request returns true then it will add a class active so it can be selected. Users know where they are on current category page.

<a href="https://www.webappfix.com" class="{{ \Request::is('/') ? 'active' : '' }}">Home</a>
<a href="https://www.webappfix.com" class="{{ \Request::is('contact') ? 'active' : '' }}">Contact</a>
<a href="https://www.webappfix.com" class="{{ \Request::is('category') ? 'active' : '' }}">Category</a>

Example 4:

Request checked in blade file if condition if true the perform something action or else will ready.

@if(\Request::is('/','home','contact'))

@else

Example 5:

Request check with URL parameters. For example users id, slug, etc...

So using * beside the path can check.

Request check with an id parameter. If the request will be home/{id} then it will return true else false.

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/home/{id}', function () {

    \Request::is('home/*');
    
    // OUTPUT => true
});

Thanks For Reading Our Post.


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :