'Laravel - check if routes on blade
I need to check 2 routes, and if one is true, add some content
@if (Route::current()->uri() != '/' || Route::current()->uri() != 'login')<div>add some content some contnt </div> @endif
I have tried with '||' and 'OR' and 'or'. I have also tried with Request::path(), which works only when checking 1 route
@if (Route::current()->uri() != '/') <div>add some content some contnt </div> @endif
If I try 2 routes it doesn't seem to work
Solution 1:[1]
You can use the built in methods to check for a route name or pattern.
See Route::is() or Route::currentRouteNamed()
For example:
Route::get('users', 'Controller..')->name('users.index');
Route::get('users/{id}', 'Controller..')->name('users.show');
Assuming you are in the following path : users/1
@if(Route::is('users.show') )
// true
@endif
@if(Route::is('users') )
// false
@endif
@if(Route::is('users.*') )
// true
@endif
So in your example try @if(Route::is('home') or Route::is('login'))..@endif
Solution 2:[2]
You could also do:
request()->routeIs('categories')
or
request()->routeIs(['categories', 'services'])
Solution 3:[3]
Try using route names instead. In your web.php file put this:
Route::get('/')->name('home');
Route::get('/login')->name('login');
And then change your if condition to this:
@if (Route::currentRouteName() != 'home' || Route::currentRouteName() != 'login')
<div>add some content some contnt </div>
@endif
Solution 4:[4]
You need to make your condition right because you're comparing between url and string. Try this plz:
URL::current() != url('/')
Solution 5:[5]
I'd use routes names and in_array instead...
In your web.php
Route::get('/', ...)->name('home');
Route::get('/login', ...)->name('login');
Then you can do something like this
in_array(Route::currentRouteName(), ['home', 'login'])
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 | DPP |
| Solution 3 | Piazzi |
| Solution 4 | Mohammed Aktaa |
| Solution 5 | Mohammed Radwan |
