Setting active class for menu items dynamically in laravel

I’m creating a laravel application using bootstrap 4.

I have my main navigation menu in app.blade.php . And I have four main blades, About,Product,Contact and Blog.

Then I needed to change the menu item active class according to the current page. As the menu being loaded dynamically from the app.blade.php I had to set the active class for current page in the navigation menu dynamically.

What I did was in every blade I’ve defined a variable called, $currentpage and assign page name in to it, assume it’s blog.blade.php

<?php $currentPage = 'blog';?>
@extends('layouts.app')

@section('content')

and in the app.blade.php,

<li class="<?php if($currentPage =='blog'){echo 'nav-item active';}?>">
<a class="nav-link " href=""></a>
</li>

So this works properly..

but I want to know Is this the correct way of doing it and what are the other possible ways to fulfill my requirement

from Newest questions tagged laravel-5 – Stack Overflow https://ift.tt/2Y93iXB
via IFTTT

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.