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

woxene's avatar

Checking current URL for active class

I am trying to add an class='active' attribute to my menu when you are on this current page. I can't seem to get this to work. Here is my code:

@if(Request::is('tickets*'))
    <ul class="tabs tabs-transparent tabs-fixed-width">
        <li class="tab"><a target="_self" href="{{ route('tickets.create') }}" @if(Request::is('tickets/create')) class='active' @endif>Ticket Entry</a></li>
    </ul>
@endif

The first If statement makes sure this menu only shows when you are in the tickets area of the website.

My url is http://127.0.0.1:8000/tickets/create and it does not add the class active.

0 likes
5 replies
vivekdhumal's avatar

Hey @woxene just do the following.

@if(Request::is('tickets*'))
    <ul class="tabs tabs-transparent tabs-fixed-width">
        <li class="tab"><a target="_self" href="{{ route('tickets.create') }}" @if(Request::is('tickets/create')) {{ "class='active'" }} @endif>Ticket Entry</a></li>
    </ul>
@endif

you forgot to echoing the class attribute using blade syntax.

vivekdhumal's avatar

okay try something like

@if(Request::is('tickets*'))
    <ul class="tabs tabs-transparent tabs-fixed-width">
        <li class="tab"><a target="_self" href="{{ route('tickets.create') }}" @if(request()->path() == 'tickets/create') {{ "class='active'" }} @endif>Ticket Entry</a></li>
    </ul>
@endif

s3nior's avatar
s3nior
Best Answer
Level 8

I work with

class="{{(Route::current()->getName() === 'tickets.create') ? 'is-active' : ''}}"
woxene's avatar

Thanks everyone for replying. It was a JS library I added editing the active classes on it's own. My first script already works.

Please or to participate in this conversation.