Laravel Blade @guest directive for multiple guards

Viewed 11700

I have the user guard (which name is web) and I have the admin guard (which name is admin). So, to show/hide stuff I use the Laravel blade directives @auth('web') or @auth('admin') but somehow the @guest doesn't work so I'm forced to use like this:

@auth('admin')
    <li>Dashboard</li>
    <li>Log out</li>
@endauth

@auth('web')
    <li>Create Post</li>
    <li>Log out</li>
@endauth

@guest('admin')
    @guest('web')
        <li>Contact</li>
        <li>About us</li>
    @endguest
@endguest

I nest the guest directives but it feels like too much, any better way of doing it?

1 Answers

Just tested this - using Laravel 5.4.36.

<ul class="nav navbar-nav">
    @guest
        <li>I'm a guest</li>
    @endauth
    <li>{{ Html::link(url('/consultants'), 'Consultants') }}</li>
    <li>{{ Html::link(url('/clients'), 'Clients') }}</li>
    <li>{{ Html::link(url('/tasks'), 'Tasks') }}</li>
</ul>

The 'I'm a guest' is only displayed to unathenticated users, as expected.

I noticed that you had @endguest and not @endauth - is that just a typo here?

Update: if you do this, does it work how you would expect?

@auth('admin')
    <li>Dashboard</li>
    <li>Log out</li>
@endauth

@auth('web')
    <li>Create Post</li>
    <li>Log out</li>
@endauth

@guest
    <li>Contact</li>
    <li>About us</li>
@endauth
Related