0

I am trying to display the User Picture in an included header template in a Laravel 8 project. Basically, I have different type of Users (Admin and Instructor) whom have different Middleware and pages. I would like to inherit the same header and display their Avatar Picture based on the User type. What I have done so far is: header.blade.php

          @php
          $instructor =  DB::table('instructors')->where('id', Auth::guard('instructor')->user()->id)->first();
          $admin =  DB::table('admins')->where('id', Auth::guard('admin')->user()->id)->first();

      @endphp
      <!-- User Account-->
      <li class="dropdown user user-menu">  
        <a href="#" class="waves-effect waves-light rounded dropdown-toggle p-0" data-toggle="dropdown" title="User">
            @if ($admin)
            <img src="{{ (!empty($admin->image))? url('upload/admin_images/'.$admin->image) : url('upload/no_image.jpg') }}" alt="">
            @elseif ($instructor)
            <img src="{{ (!empty($instructor->image))? url('upload/admin_images/'.$instructor->image) : url('upload/no_image.jpg') }}" alt="">
            @else
            <img src="{{ asset('images/1.jpg') }}" alt="">
            @endif

The error is giving me is:

Trying to get property 'id' of non-object

This error is showing ONLY when I try to login as an Instructor. The Middleware, login controller for both User type are basically the same. The Instructor login is working perfectly ONLY if I completely remove the $admin variable from header.blade.php. I tried also to put in different orders the if and elseif condition but nothing changed. Does anybody has any clue on how to solve it?

miken32
  • 42,008
  • 16
  • 111
  • 154
Giorgio Scarso
  • 386
  • 1
  • 2
  • 11
  • Dump Auth::guard('instructor')->user() and see if it's null. Maybe you should check what kind of user is logged in first in order to make the right query. – Emil Georgiev May 18 '22 at 21:50
  • The dump is working fine. I mean, the user is right. In fact, if I remove the admin variable in the template, the instructor page works. – Giorgio Scarso May 18 '22 at 21:58
  • 2
    you are trying to get the user from 2 different guards ... most likely you are only authenticated with 1 guard so the other will be returning `null`, which is not an object – lagbox May 18 '22 at 22:06
  • 1
    I'm pretty sure that @lagbox 's answer is the correct. That's why I was asking did you try to dump the guard and the user to see is it null or not. – Emil Georgiev May 18 '22 at 22:15
  • Why the user type is not an attribute of the same user? If you can get the rol of the current user you can call directly on the image source eg. url('upload/images/'.$user->rol.'.jpg'); – Manuel Eduardo Romero May 19 '22 at 00:29

1 Answers1

1

First of all, never use @php in your Blade templates. Logic absolutely does not belong in your views, they are strictly for displaying information. If you do need access to a variable in your view, assign it in the controller and pass it to the view.

Your error is occurring because you're attempting to get information on two different guards, when a user will only ever have one or the other. Blade provides the ability to check the current user's guard with the @auth directive.

You can specify specific guards, but that is irrelevant since you have the same code inside each branch of your conditional

<!-- User Account-->
<li class="dropdown user user-menu">  
    <a href="#" class="waves-effect waves-light rounded dropdown-toggle p-0" data-toggle="dropdown" title="User">
        @auth
        <img src="{{ url('upload/admin_images/' . Auth::user()->image ?? '../no_image.jpg') }}" alt="">
        @endauth
        @guest
        <img src="{{ asset('images/1.jpg') }}" alt="">
        @endguest
    </a>
</li>

But, should you need to check for a specific guard, use this syntax:

<div>
Welcome, you are authenticated as 
@auth('admin')
an administrator
@auth('instructor')
an instructor
@endauth
</div>
miken32
  • 42,008
  • 16
  • 111
  • 154