skip to Main Content

I am very new to Laravel and am using version 8. This is probably a simple question but I’m struggling to find a decent, simple answer.
I have a layout view/template called app.blade.php. Inside it, I want to check if the user is logged in and the role id. Based on the user’s role, I want to display the specific navbar for that role.

Here’s what I had initially, but it obviously doesn’t work, all navbars are displayed and I understand why. What I’m looking for is something like an include statement from vanilla PHP.

@if (Auth::check())
  @if(Auth::user()->role_id == 1)
      @extends('layouts.adminnav')
  @elseif (Auth::user()->role_id == 2)
      @extends('layouts.devnav')
  @else
     @extends('layouts.usernav')
  @endif
@endif

Thanks for any pointers.

2

Answers


  1. Chosen as BEST ANSWER

    I actually found the problem. @include instead of @extends inside the if statement. It won't respect the condition results if I use @extends and I believe this is by design. Any way, the solution looked like the following:

     @if (Auth::check())
        @if(Auth::user()->role_id == 1)
          @include('layouts.adminnav')
        @elseif (Auth::user()->role_id == 2)
          @include('layouts.devnav')
        @else
         @include('layouts.usernav')
        @endif
    @endif    
    

  2. According to your if the else statements, you are insisting not to see your user navbar when you log in. That is probably what is happening. You can see your navbar if you are already logged in.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search