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.
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.
Hey @vivekdhumal, unfortunately that doesn't solve the problem. Any other suggestions?
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
I work with
class="{{(Route::current()->getName() === 'tickets.create') ? 'is-active' : ''}}"
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.