Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ponnydalen's avatar

Blade directive if route is not include blade component

Hello. I'm trying to make a custom blade directive which will check current route and only show component IF route is not equals to..

So far tried with this and this works :

// AppServiceProvider
Blade::if('routeisnot', function ($route) {

            $rule = \Request::route()->getName() != $route;
            
            return $rule;
        });

// Layout file
@routeisnot('homepage')
	@include('client/components.booking-bar')
@endrouteisnot

But I would like to put multiple routes in there, like an array

Example:

@routeisnot(['homepage', 'booking', 'login', 'register'])
	@include('client/components.booking-bar')
@endrouteisnot

How could I achieve this? Any ideas? Thanks.

0 likes
7 replies
Sinnbeck's avatar

If I understand you correctly it should work both with arrays and strings?

Blade::if('routeisnot', function ($route) {
           $name = \Request::route()->getName();
            if (is_array($name, $route) {
                  return !in_array($route);
            }

            return $name != $route;

        });
ponnydalen's avatar

Yes, correct. should work with both arrays and string. But it gives me this where I : return !in_array($route) :

in_array() expects at least 2 parameters, 1 given

And this is how i put it in blade:

@routeisnot(['homepage', 'login'])
                @include('client/components.booking-bar')
            @endrouteisnot
automica's avatar

@ponnydalen you can simply your directive as such:

Blade::if('routeisnot', function ($route) {

    $name = \Request::route()->getName();

    if (is_array($route) && in_array($name, $route)) {
        return $name != $name;
    };

    return $name != $route;
});
ponnydalen's avatar

Yeah, I got it working now. I think @sinnbeck just forgot $name parameter. Thank you both very much.

Final soloution:

Blade::if('routeisnot', function ($route) {

            $name = \Request::route()->getName();
            
            if(is_array($route)) {

                return !in_array($name, $route);
            }
            return $name != $route;
        });
Sinnbeck's avatar

Sorry fixed my answer :) Forgot the $name key.

MichalOravec's avatar
Level 75

@ponnydalen In my opinion it's same as

@includeWhen(! in_array(Route::currentRouteName(), ['homepage', 'login']), 'client.components.booking-bar')

or

@includeUnless(in_array(Route::currentRouteName(), ['homepage', 'login']), 'client.components.booking-bar')

And for one route

@includeWhen(Route::is('homepage'), 'client.components.booking-bar')
@includeUnless(Route::is('homepage'), 'client.components.booking-bar')

Or this is also same what you had before

@includeUnless(Route::is('homepage', 'login'), 'client.components.booking-bar')
ponnydalen's avatar

Ohboy. I didn't know about these directives. Thanks for the soloution Michal!

Please or to participate in this conversation.