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

luk's avatar
Level 1

How to navigate differnet sidebar items depending on daynamic roles ?

Hello ,I am using laravel8 and spatie package for my user management system and i can create different roles dynamical as i want , but what I want is to navigate different sidebar items depends on the users role in the system fro example if the logged in user has the role "admin" there maybe the user management and the role management items in the sidebar and if the user has the "is_darictor" role there may not be those items like in the admins sidebars so how can i control this view using the spatie package in my project! since i am new for this package any one who can help me please!

0 likes
6 replies
luk's avatar
Level 1

@aleahy i know that but as i explain the role is dynamic which means it can be created any time and i want manage it dynamical the sidebar menu that is what i want

aleahy's avatar

@luk The parameter passed into the directive is just a string. You can just pass whatever dynamic roles exist as a collection of strings to your view. Eg:

@foreach($roles as $role)
    @role($role)
       ...
    @endrole
@endforeach
PovilasKorop's avatar

@luk navigating items should depend not on the role, but on the permission.

So instead of having @role('admin') in the Blade, which is not dynamic, you should do @can('manage_users') which is permission and is more dynamic. And behind the scenes, you dynamically manage what roles/users have access to that permission.

I have a few videos with demo projects using the Spatie Permission package:

1 like
luk's avatar
Level 1

@PovilasKorop so if i have 100 permissions ,then there may be 100 @can("permissions") codes in my blade file in order to manage the view ??

PovilasKorop's avatar

@luk depends on the structure of your permissions and menu items, but yes, it's pretty normal.

Not sure how else do you imagine more flexibility, that Laravel should "guess" your permissions somehow?

Please or to participate in this conversation.