'make links active in laravel

I have this list of links in Laravel 5.3

@foreach($categories as $category)
     <li class="list-group-item text-right"><a href="{{url('/product/'.$category->id)}}">{{$category->name}}</a></li>
@endforeach

How can I make the current link active



Solution 1:[1]

You can use Request if the current one matches when you are printing the li and then include the class active.

This will add active class when the URI is /product/1.

{{ Request::is('/product/1') ? 'active' : null }}

In your code:

@foreach($categories as $category)
     <li class="list-group-item text-right {{ Request::is('/product/'.$category->id) ? 'active' : null }}"><a href="{{url('/product/'.$category->id)}}">{{$category->name}}</a></li>
@endforeach

Solution 2:[2]

@foreach($categories as $category)
     <li class="list-group-item text-right"><a href="{{url('/product/'.$category->id)}}" class="{{ Request::is('/product/'.$category->id) ? 'active' : null }}">{{$category->name}}</a></li>
@endforeach

That is what you need class should be applied to link not li

Solution 3:[3]

I did not find any good simple solution how to do that. But I came up with my solution. You can use it in Laravel 5.6 and it work very well.

@foreach($categories as $category)

<li class="list-group-item @if(Request::fullUrl() === route('category.index', strtolower($category->name))) active @endif">
<a href="{{ route('category.index', strtolower($category->name))}}">{{$category->name}}</a></li>

@endforeach

Solution 4:[4]

I personally used this one.

class="{{ request()->routeIs('user.dashboard') ? 'active' : '' }}"

And to make active on multiple routes.

class="{{ request()
    ->routeIs('user.dashboard', 'user.channels') ? 'active' : '' }}"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Sandip Patel
Solution 3 macalu
Solution 4 Karl Hill