0

I'm trying to utilize the Laravel landing page route. For example Laravel takes you to the welcome page by default, and it has no url text after the slash.

Route::get('/', function () {
    return view('welcome');
});

In my welcome page I'm using following condition to apply styling based on the route name.

{!! Route::is('/')? 'class="index"':'' !!}

However this code doesn't work. How can I check the route of the welcome page properly?

Edit: Using "Request" instead of "Route" makes it work. However for consistency's sake I would like to know if it can be done using "Route" too.

Nooglers BIT
  • 71
  • 2
  • 11
  • check this https://stackoverflow.com/questions/30046691/how-to-get-current-route-name-in-laravel-5 – rkj Jun 21 '18 at 09:26

5 Answers5

1

try this

@if(Request::is('/'))
  class="index"
@endif

or more simply

if(Request::is('/')) {
   class="index"
}
Joyal
  • 2,587
  • 3
  • 30
  • 45
1

Try this:

<li class="@if (request()->is('/')) index @endif"></li>
    // ...
</li>

You can use wildcards as well with this:

<li class="@if (request()->is('/some-url/*')) active @endif">
    // ...
</li>
Robert
  • 5,703
  • 2
  • 31
  • 32
1

Name your route and use this name to condition your styling

Route::get('/', function () {
    return view('welcome');
})->name('home');

In your blade

{!! (Route::currentRouteName() == 'home')? 'class="index"':'' !!}
OpenStark
  • 476
  • 2
  • 8
1

Try this:

<li {!! Request::is('/') ? 'class=index' : '' !!}>...</li>
Justinus Hermawan
  • 1,194
  • 1
  • 23
  • 44
1
Route::get('/', function () {
    return view('welcome');
})->name('home');

in blade

class = "default class @if(\Request::route()->getName() == 'home')your_class @endif"