Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Jecs9's avatar
Level 1

Dynamically activate navbar ignoring query string

Hello,

I am trying to activate the current navigation button depending on the active request using {{ Request::is(dashboard/xxxx) ? 'active' : '' }}

I have few pages in the dashboard and some of them have tables, those tables use query strings to identify the page, orderBy specific column and order direction (asc, desc)

The menu get activated properly if there is no query string but as soon as there is then the button is no longer active

For example, if I go directly to dashboard/users then the button is active, but if I order by some column or if I change to page 2 and the url is for example dashboard/users?sortField=referrer.name&sortDirection=asc then the navigation button of dashboard/users is no longer active

Is there a way to ignore the query string to keep showing the navigation button active? I am using Laravel 8

Thank you in advance for your help

0 likes
12 replies
Jecs9's avatar
Level 1

@webrobert Thank you for the reply, I have tried it and it does the same

The route name is dashboardUsers and if there is a query string then it doesn't work

webrobert's avatar

@Jecs9,

if its named dashboardUsers then use...

request()->routeIs('dashboardUsers')

EDIT:

Say for example you have a forum like this one. And you want Discussions to be active on both the index and the show pages. And those routes are named discussion.index and discussion.show You would use...

request()->routeIs('discussion.*')

This works regardless of querystring.

Jecs9's avatar
Level 1

@webrobert

I am using this directly in the class of the button {{ Request::routeIs('dashboardUsers') ? 'active' : '' }}

I can see how the button is active if there is no query string But when I have a query string then the button is no longer active

The url after .com is /dashboard/users?sortField=referrer.name&sortDirection=asc

As soon as anything is placed after users then it doesn't work

The route is

Route::get('/dashboard/users', Users::class)->name('dashboardUsers');

Jecs9's avatar
Level 1

@webrobert Yes the button itself is inside a livewire component but the button is just for navigation so there is no wire connected to it

I have a blade component that is the dashboard, then I have livewire components for each page inside the dashboard because I wanted to use the $slot of the blade that was not available in livewire components

webrobert's avatar

@Jecs9, it won’t work in the component because when the livewire cycle happens it changes the route/url of the component

See for yourself. After clicking it will be the route that Livewire uses…

@dump(request()->route()->getName())
Jecs9's avatar
Level 1

@webrobert Oh yes I see, it changes to "livewire.message"

Is there any way to get this to work? Or with livewire is not possible?

webrobert's avatar

@Jecs9, I’m not entirely sure what you’re trying to do beyond this one button. But you could pass the route into the component as a variable

Jecs9's avatar
Level 1

@webrobert Yes I could try but I think that will not work

I have the dashboard page that is a blade component, here I have all the buttons to navigate through the dashboard pages, in the $slot of that blade component I am rendering the livewire component of each dashboard sub page

So when I click for example the button to go to the dashboard/users page, the users button shows active. In that page I have a table showing the users, when I click in the livewire component to rearrange the columns or to change page, then the url now becomes a livewire.message and the users navigation button becomes inactive

So the navigation button is outside of the livewire component but when I interact with the livewire component the url becomes a livewire.message

webrobert's avatar
Level 51

@Jecs9,

but when I interact with the livewire component the url becomes a livewire.message.

It must be living within a livewire component that changes because the rest of the page will be static. Livewire won't change it. So the solution is one of the following...

  1. Move the buttons outside the Livewire component, if they truly aren't interacting with the Livewire functionality. OR
  2. Define the route on mount in the Livewire component and its related blade...
public $currentDash;

public function mount()
{
    $this->currentDash = Route::currentRouteName();
	...
}

and for the button(s)...

{{ $currentDash === 'dashboardUsers' ? 'active' : '' }}
// and so on
Jecs9's avatar
Level 1

@webrobert Thank you, that is what I had to do, I had to reorganize the project and move the buttons out of the livewire component

1 like

Please or to participate in this conversation.