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

Asked

Viewed 553 times

0

I’m using Laravel 5.6 along with the Adminlte. When I log in it is returning me the error

Errorexception (E_ERROR) Trying to get Property of non-object

Indicating the view page.blade.php, in the stretch

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

I tried using 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',
   ];

}

1 answer

2


You are trying to obtain properties from a user who there is no (not logged in), to resolve you need to verify that the user is logged in example:

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

I mean, he’s just gonna do output those two paragraphs when the user is logged in.

  • 1

    I’ll test it here, vlw!

  • After a long time thinking and reviewing the code I found that it was problems in the routes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.