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

Ashraam's avatar

Roles and sidebar

Hi everyone,

I'm working on a project where there are different roles (admin, manager, user, ... in the end there will be 9 differents roles) Each user has only one role but there all working on the same app.

Do you know a simple way (or at least the most practical) to adjust the sidebar with the role of the user logged ?

Ex: An admin will have access to all features, a manager will have to the users and can only edit them, etc....

Am i clear enough ?

Thanks for your help !

0 likes
9 replies
Joeri's avatar

If they have a role_id you can check for the right permissions in the controller Auth::user()->role_id > 2 and you can make a if in the view to display html @if(Auth::user()->role_id > 2)

Ashraam's avatar

This is the method I was thinking in the first time so it means if there is 8 different roles, I'll need 8 different sidebars ?

Joeri's avatar

No not realy, Here is a code example from project im working on, maybe that explains it

<span class="check checkFile">
    @if(Auth::user()>role_id < 3 || Auth::user()->id == $file->user_id)
        <input type="checkbox" class="file_delete" value="{{$file->id}}">
     @else
         <span class="empty-box"></span>
    @endif
</span>
Ashraam's avatar

Yes, I already put the sidebar in a partial view.

<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
</ul>

Admin will have access to everything Manager will have access to 1,2,3,5 Technician will have access to 1,3,4 etc...

So I'm wondering, is it better to include a admin.sidebar if you're Admin, manager.sidebar... or just doing multipe; if on the same sidebar like

@if (in_array(auth()->user()->role, ['Admin', 'Manager']))
    <li>2</li>
@endif
Joeri's avatar

If there is much difference between the roles sidebar that is an option

@if (auth()->user()->role =  'Admin')
    @include('sidebars.admin')
@elseif (auth()->user()->role =  'Manager')
    @include('sidebars.manager')
@endif
sutherland's avatar

If you split the sidebar into different files, I would use something like

@include('sidebars.'.auth()->user()->role)

to keep things a little cleaner.

Another good method would be to define abilities for your roles and just check if the user can perform the action.

@can('update', $foo)
    <li>Update</li>
@endcan
@can('delete', $foo)
    <li>Delete</li>
@endcan
@can('approve', $foo)
    <li>Approve</li>
@endcan

The benefit of this is that you can have one central location for your abilities, and if a new sidebar item comes along you don't have to add it to multiple files. Just define the new ability, and add it to your sidebar template.

jekinney's avatar

As @sutherland pointed out I would suggest a permissions style roles and permissions.

That roles and more importantly role updates are irrelevant (more so if you add or remove a role later) your deligating roles as a permissions group and permissions define access. Instead of checking for 2 different roles your checking for on permission for access and it won't matter what role.

1 like
Ashraam's avatar

Thank you guys for your answers. I think the best way is to create an ACL but I really don't how to to start this..

I'll dig into that but if you have any tutorials I wouldn't mind ;)

Thanks again

Please or to participate in this conversation.