Variable returns NULL - Trying to get property of non-object

0

I'm using laravel 5.6 along with the AdminLTE . When I login, it is returning the error

  

ErrorException (E_ERROR)   Trying to get property of non-object

Displaying the page.blade.php view, in the

 <div class="pull-left info text-center">                    
    <p>{{ Auth::user()->name}}</p>
    <p>Crc {{ Auth::user()->crc}}</p>
 </div>

I tried to use the auth () -> user () -> name class, but the same problem occurs.

Model Users

class User extends Authenticatable
{
   use Notifiable;

   /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
   protected $fillable = [
    'name', 'email','cpf', 'crc', 'password', 'cep', 'endereco', 'numero',
    'bairro', 'cidade',
   ];

   /**
    * The attributes that should be hidden for arrays.
    *
    * @var array
    */
   protected $hidden = [
    'password', 'remember_token', 'cpf', 'crc',
   ];

}
    
asked by anonymous 29.06.2018 / 15:20

1 answer

2

You are trying to get properties of a user that does not exist (not logged in), to resolve you need to verify that the user is logged in

 <div class="pull-left info text-center">
    @if(Auth::check())                 
    <p>{{ Auth::user()->name}}</p>
    <p>Crc {{ Auth::user()->crc}}</p>
    @endif
 </div>

That is, it will only output of these two paragraphs when the user is logged in.

    
29.06.2018 / 18:33