Search code examples
phplaravelguard

How to check which guard we are currently within in laravel


I have here a portion of my header.blade.php file of a project.

                    @guest   
                    @else
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true">
                            {{ Auth::user()->name }} <span class="caret"></span>
                            </a> 
                             @if(condition here)
                             $profile='/myprofile';
                             $logout='/logout';
                             @else
                             $profile='/admin/myprofile';
                             $logout='/admin/logout';
                             @endif
                            <ul class="dropdown-menu">
                                  <li><a href="{{$profile}}">My Profile</a></li>
                               <li>  <a href="{{$logout}}">Log Out</a>                                   
                                </li>
                            </ul>
                              @endif
                        </li>
                    @endguest

I have 2 guards namely web and admin. I want the $profile and $logout to be '/profile' and '/logout' && '/admin/profile' and '/admin/logout' respectively for each of the 2 guards.

Now I know that I can check for each guard as Auth::guard('name')->check(); But, when both the guards are logged in at the same time, it creates a sort of problem this way.i.e.

     if(Auth::guard('web')->check())
{$profile="/myprofile";}
if(Auth::guard('admin')->check())
{$profile="/admin/myprofile";}

Since both will be active, $profile will have the latter value in every case. So, is there a way such that this problem can be addressed? Something like

if(Auth::guard()=='admin'){
////////////////
}

which of course doesn't work.


Solution

  • I guess youll have to have separate headers for each of the interfaces taken care by each of your guards. Clearlyif(Auth::guard()=='admin'){ //////////////// } is not valid without mentioning the guard name