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

moazam's avatar

Building a navigation for user roles in laravel

Hi everyone,

I am making a backend app where I want to display a different navigation for each user role. I am building ACL with entrust package. I am facing two issues here:

  1. How to create navigation that easier to manage and only allow users to view assigned pages
  2. How to display navigation i.e. create a file for each role, etc. and display if user role matches.

Any help is greatly appreciated.

Thanks

0 likes
4 replies
davidfaux's avatar

Creating a file for each role should be quite simple however there will be some repetition of links across files. What I have done in the past (not a ideal solution either I feel) was to include navigation file if the role was allow to view, e.g.

@if(\Auth::user()->is('Admin')
    @include('nav.admin.users')
    @include('nav.admin.accounts')
@endif
@if(\Auth::user()->is('Manager')
    @include('nav.manger.staff')
@endif
//etc, etc

I can't recall the methods used to retrieve the role from the user so replace is with the equivalent

absiddiqueLive's avatar

You may follow the style that provided by fresh Laravel

@if (Auth::guest())
    <li><a href="{{ url('/login') }}">Login</a></li>
    <li><a href="{{ url('/register') }}">Register</a></li>
@else 
    <li><a href="{{ url('/home') }}">Home</a></li>
    <li><a href="{{ url('/Profile') }}">Profile</a></li>
@endif 

@if(Auth::user()->is('Manager')) {{-- just test --}}
    <li><a href="{{ url('/manage') }}">Manage</a></li>
@endif 
moazam's avatar

Thanks for your suggestions.

What's the best way to select current page active i.e. if user is on a profile page then I would like to select that menu active.

bestmomo's avatar

I set and manage a session variable for user statut, so everywhere in views I can easily select what must be showed or not :

@if(session('statut') == 'admin')
    {!! link_to_route('admin', trans('back/admin.administration'), [], ['class' => 'navbar-brand']) !!}
@else
    {!! link_to_route('blog.index', trans('back/admin.redaction'), [], ['class' => 'navbar-brand']) !!}
@endif 

Please or to participate in this conversation.