0

Am using same navbar for shop products and shop product. @if statement isn't working. Here is navbar:

<ul class="navbar-nav ml-auto">
      <li class="nav-item">

        @if (\Request::path('/shop'))
        <a class="nav-link js-scroll-trigger" href="#page-top">Shop</a>
        @elseif (\Request::path('/product'))
        <a class="nav-link js-scroll-trigger" href="/shop">Shop</a>             
        @endif

      </li>
      <li class="nav-item">
        <a class="nav-link js-scroll-trigger" href="#contact">Contact</a>
      </li>
    </ul>

Here is routes:

Route::view('/product', 'shop.product');
Route::get('/shop', 'ShopController@index')->name('shop.index');
James Pike
  • 171
  • 1
  • 2
  • 9
  • Looks like a dupe question. https://stackoverflow.com/questions/17591181/how-to-get-the-current-url-inside-if-statement-blade-in-laravel-4 – bassxzero Apr 28 '19 at 03:11

1 Answers1

0

You can use request()->is() function of request. as below:

@if (request()->is('shop'))
    <a class="nav-link js-scroll-trigger" href="#page-top">Shop</a>
@elseif (request()->is('product'))
    <a class="nav-link js-scroll-trigger" href="/shop">Shop</a>             
@endif
Imran
  • 4,582
  • 2
  • 18
  • 37
  • Thanks Imran, followed the dupe question link but code still didnt work. Made a controller to bring in the request function (not sure whether was suppose to or not), then tried your request()->is() function code and it works...Cheers – James Pike Apr 28 '19 at 05:01