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

myke2424's avatar

displaying navbar according to user roles

I want to display a different navigation bar for the Admin user. I've added an 'is_admin' attribute to my user model. It's a boolean value, if 'is_admin' = 1, the user is an admin. Here's the model

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'is_admin',
    ];

Here's the current nav bar

<div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <!-- Left Side Of Navbar -->
                    <ul class="navbar-nav mr-auto">

                    </ul>

                    <!-- Right Side Of Navbar -->
                    <ul class="navbar-nav ml-auto">
                        <!-- Authentication Links -->
                        @guest
                            <li class="nav-item">
                                <a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
                            </li>
                            @if (Route::has('register'))
                                <li class="nav-item">
                                    <a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
                                </li>
                            @endif

                            <li class="nav-item">
                                <a class="nav-link" href="/timetracker">Time</a>
                            </li>

                            <li class="nav-item">
                                <a class="nav-link" href="/reports/create">Reports</a>
                            </li>

                            <li class="nav-item dropdown">
                                <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
                                    {{ Auth::user()->name }} <span class="caret"></span>
                                </a>

                                <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
                                    <a class="dropdown-item" href="{{ route('logout') }}"
                                       onclick="event.preventDefault();
                                                     document.getElementById('logout-form').submit();">
                                        {{ __('Logout') }}
                                    </a>

                                    <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                                        @csrf
                                    </form>
                                </div>
                            </li>
                        @endguest
                    </ul>
                </div>
            </div>
        </nav>

I want it so the regular user will have the current nav-bar items, and the is_admin user will have one nav-item called:

 <li class="nav-item">
                                <a class="nav-link" href="/Admin">Admin</a>
 </li>

How could I do this?

0 likes
10 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You seem to know all that is needed

Just use if to render your admin menu

@if(Auth::user()->is_admin)
<li class="nav-item">
                                <a class="nav-link" href="/Admin">Admin</a>
 </li>
@endif
Nakov's avatar

just by adding a condition?

@if( auth()->check() && auth()->user()->is_admin)

// your nav item here

@endif
myke2424's avatar

@Nakov thank you. I tried that before the post but it wasn't working, must of been a typo.

Nakov's avatar

@myke2424 sure np, glad you resolved it.. It is exactly the same as using the facade, plus a check that a user is logged in.

Sinnbeck's avatar

But if it is inside an Auth middleware route check should not be needed. If it's not, it's a good idea to use it

Sinnbeck's avatar

@nakov ah nice catch!

Just noticed this which would give the same issues (I assume Auth::user() returns null if not logged in)

{{ Auth::user()->name }}

Should be

{{ auth()->check() ? auth()->user()->name : ''}}

Or

{{auth()->user()->name ?? ''}}
Nakov's avatar

@sinnbeck again this one

{{auth()->user()->name ?? ''}}

no, because calling name on null will throw exception..

This might be

{{ optional(auth()->user())->name }}
Sinnbeck's avatar

@nakov Try running this in tinker :) Does not throw an exception here

auth()->user()->foo ?? 'bar';
1 like

Please or to participate in this conversation.