vahan terzibashian vahan terzibashian - 9 months ago 91
PHP Question

Laravel Request::is() - is there a better way?

@if(Request::is('login') OR Request::is('tags') OR Request::is('categories') OR Request::is('posts') OR Request::is('tags/..') OR Request::is('categories/..') OR Request::is('posts/..') OR Request::is("posts/{$post->id}"))

here is an example in my main.blade.php file, now what I'm doing is that I am trying to use 2 different navigation bars - I know there is a better way to do this but I still can't get the grasp of it!

I don't think it is good coding standards to repeat Request::is over and over again. I am a newbie :( what did I miss over there?

Answer Source

is() method iterates over arguments:

foreach (func_get_args() as $pattern) {
    if (Str::is($pattern, $this->decodedPath())) {
        return true;

So, something like this should work for you:

@if(Request::is('login', 'tags', 'categories', 'posts', 'tags/..', 'categories/..', 'posts/..', 'posts/{$post->id}'))