Laravel: Auth::user()->id trying to get a property of a non-object
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;
Related videos on Youtube
Josh
Updated on November 19, 2021Comments
-
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 almost 11 yearsIs there a column named id in your User table? If not, that's the issue.
-
Josh almost 11 yearsYep, id is there @OffTheFitz the table is called users
-
Altrim almost 11 yearsCorrect 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 almost 11 years@Josh What about your model. Whats the protectedKey?
-
Josh almost 11 yearsHey @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 almost 11 yearsWell if you are using Sentry check the logged in user with
Sentry::getUser()->id
. The error you get is that theAuth::user()
returns NULL and it tries to get id from NULL hence the errortrying to get a property from a non-object
. -
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 over 10 yearsThat's correct - first you must to Auth::check() if user is loggin. Then the Auth::getUser() will have an useful object :)
-
Cato Minor almost 9 yearsIf 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 almost 8 yearsI prefer to use method chaining {{auth()->user()->name}}
-
RN Kushwaha over 5 years
Auth::id()
is working fine in laravel 5.7 version. -
gumuruh over 4 yearswhat if laravel 6 ?
-
rashedcs over 4 yearsvery elegant answer.
-
Barkermn01 over 4 yearsWhile 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 over 4 yearsHi 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 about 4 yearsAuth::id() is wordking Laravel 6.0
-
mauronet over 3 yearsDon't forget to add "use Illuminate\Support\Facades\Auth;"
-
Mohamed Raza over 3 years
Auth::user()->role
Solved my issue.use Illuminate\Support\Facades\Auth;
is required to work withAuth::user()
. Thank you for your answer -
Jem about 2 years@maronet in later version of Laravel you can simply do
use Auth;
and you'll get the same result.