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

sork's avatar
Level 1

showing navigation menu items based on user role

I am having a weird issue when showing navigation menu items based on the user role. at the home page each registered user become Admin, while in other pages (example posts page) the user gets correct role.

Please note, for checking admin and other roles in User model; I am using below:

public function isAdmin()
    {
        return $this->role->name == 'Admin';
    }

public function isSupervisor()
    {
        return $this->role->name == 'Supervisor';
    }

role relationship in user model is:


public function role()
    {
        return $this->belongsTo(Role::class);    
    }

and in master layout, I use below code to check if the user is admin:

@if(auth()->user()->isAdmin())

	//show nav items that admin can see
@endif

Appreciate if anyone can guide me to get a solution.

0 likes
2 replies
Nakov's avatar
Nakov
Best Answer
Level 73

I would define some gates and use those instead: https://laravel.com/docs/master/authorization#writing-gates

So in your AuthServiceProvider add this:

 // use Illuminate\Support\Facades\Gate; -- add this at the top

Gate::define('admin', function ($user) {
    return $user->role->name === 'Admin';
});

and use it in your view like:

@can('admin')

// navigation

@endcan

or in the controller:

$this->authorize('admin');

or as in the documentation link I shared above.

Please or to participate in this conversation.