Laravel: Auth::user()->id trying to get a property of a non-object

500,471

Solution 1

If you are using Sentry check the logged in user with Sentry::getUser()->id. The error you get is that the Auth::user() returns NULL and it tries to get id from NULL hence the error trying to get a property from a non-object.

Solution 2

Now with laravel 4.2 it is easy to get user's id:

$userId = Auth::id();

that is all.

But to retrieve user's data other than id, you use:

$email = Auth::user()->email;

For more details, check security part of the documentation

Solution 3

Do a Auth::check() before to be sure that you are well logged in :

if (Auth::check())
{
    // The user is logged in...
}

Solution 4

id is protected, just add a public method in your /models/User.php

public function getId()
{
  return $this->id;
}

so you can call it

$id = Auth::user()->getId();

remember allways to test if user is logged...

if (Auth::check())
{
     $id = Auth::user()->getId();
}

Solution 5

In Laravel 8.6, the following works for me in a controller:

$id = auth()->user()->id;
Share:
500,471

Related videos on Youtube

Josh
Author by

Josh

Updated on November 19, 2021

Comments

  • Josh
    Josh over 2 years

    I'm getting the following error "trying to get a property of a non-object" when I submit a form to add a user, the error is apparently on the first line: Auth::user()->id of the following:

    $id = Auth::user()->id;
    $currentuser = User::find($id);
    $usergroup = $currentuser->user_group;
    $group = Sentry::getGroupProvider()->findById($usergroup);
    
    $generatedPassword = $this->_generatePassword(8,8);
    $user = Sentry::register(array('email' => $input['email'], 'password' => $generatedPassword, 'user_group' => $usergroup));
    
    $user->addGroup($group);
    

    Any ideas? I've searched for a while and everything I see says this should work fine. My user is logged in using the Sentry 2 authentication bundle.

    • OffTheFitz
      OffTheFitz almost 11 years
      Is there a column named id in your User table? If not, that's the issue.
    • Josh
      Josh almost 11 years
      Yep, id is there @OffTheFitz the table is called users
    • Altrim
      Altrim almost 11 years
      Correct me if I am wrong but you are trying to register a logged in user?? If you are using Sentry to register a user why are you calling Auth::user();? the Auth::user() returns the current logged in user, so you are trying to register a user which should be logged in, it doesn't make sense
    • OffTheFitz
      OffTheFitz almost 11 years
      @Josh What about your model. Whats the protectedKey?
    • Josh
      Josh almost 11 years
      Hey @Altrim I'm actually trying to let an admin user register a new user (the form accepts the input of an email address) and giving that new user the same group as the admin's user_group. Later on the new users get an email to activate their account. It worked fine until I tried adding the group to the mix.
    • Altrim
      Altrim almost 11 years
      Well if you are using Sentry check the logged in user with Sentry::getUser()->id. The error you get is that the Auth::user() returns NULL and it tries to get id from NULL hence the error trying to get a property from a non-object.
    • Josh
      Josh almost 11 years
      @Altrim ah, so silly of me, that works fine! Please chuck it in as the answer so I can vote it in as the correct one. Thank you to you too OffTheFitz.
  • BlueMan
    BlueMan over 10 years
    That's correct - first you must to Auth::check() if user is loggin. Then the Auth::getUser() will have an useful object :)
  • Cato Minor
    Cato Minor almost 9 years
    If you want to do this using blade templating: @if(Auth::check()) {{Auth::user()->id}} @endif (this is useful if you want to get the user's ID for javascript / ajax)
  • mercury
    mercury almost 8 years
    I prefer to use method chaining {{auth()->user()->name}}
  • RN Kushwaha
    RN Kushwaha over 5 years
    Auth::id() is working fine in laravel 5.7 version.
  • gumuruh
    gumuruh over 4 years
    what if laravel 6 ?
  • rashedcs
    rashedcs over 4 years
    very elegant answer.
  • Barkermn01
    Barkermn01 over 4 years
    While this answer might technically be correct please provide a context as to why you think this would fix the problem and what it does E.G (When using namespaces anything in the global namespace needs to have a `` before it to denote it's in the global namespace and not the current namespace)
  • Plutian
    Plutian over 4 years
    Hi and welcome to stackoverflow, and thank you for answering. While this code might answer the question, can you consider adding some explanation for what the problem was you solved, and how you solved it? This will help future readers to understand your answer better and learn from it.
  • Pavel8289
    Pavel8289 about 4 years
    Auth::id() is wordking Laravel 6.0
  • mauronet
    mauronet over 3 years
    Don't forget to add "use Illuminate\Support\Facades\Auth;"
  • Mohamed Raza
    Mohamed Raza over 3 years
    Auth::user()->role Solved my issue. use Illuminate\Support\Facades\Auth; is required to work with Auth::user(). Thank you for your answer
  • Jem
    Jem about 2 years
    @maronet in later version of Laravel you can simply do use Auth; and you'll get the same result.