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

PhpAuthenticationLaravelLaravel 4

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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 2 - Php

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 3 - Php

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

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

Solution 4 - Php

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 - Php

In Laravel 5.6 I use

use Auth;
$user_id = Auth::user()->id;

as the other suggestion

Auth::id()

seems to apply to older versions of Laravel 5.x and didn't work for me.

Solution 6 - Php

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

$id = auth()->user()->id;

Solution 7 - Php

Never forget to include and try to use middleware auth:

use Illuminate\Http\Request;   

Then find the id using request method:

$id= $request->user()->id;

Solution 8 - Php

Check your route for the function in which you are using Auth::user(), For getting Auth::user() data the function should be inside web middleware Route::group(['middleware' => 'web'], function () {}); .

Solution 9 - Php

You may access the authenticated user via the Auth facade:

use Illuminate\Support\Facades\Auth;

// Get the currently authenticated user...
$user = Auth::user();

// Get the currently authenticated user's ID...
$id = Auth::id();

Solution 10 - Php

If anyone is looking for laravel 5 >

 Auth::id() 

give id of the authorized user

Solution 11 - Php

use Illuminate\Support\Facades\Auth;

In class:

protected $user;

This code it`s works for me

In construct:

$this->user = User::find(Auth::user()->id);

In function:
$this->user->id;
$this->user->email;

etc..

Solution 12 - Php

you must check is user loggined ?

Auth::check() ? Auth::user()->id : null

Solution 13 - Php

It's working with me :

echo Auth::guard('guard_name')->user()->id;

Solution 14 - Php

Laravel 8 solution:

use Illuminate\Support\Facades\Auth;

// Retrieve the currently authenticated user...
$user = Auth::user();

// Retrieve the currently authenticated user's ID...
$id = Auth::id();

For more details, see the latest docs here

Solution 15 - Php

Your question and code sample are a little vague, and I believe the other developers are focusing on the wrong thing. I am making an application in Laravel, have used online tutorials for creating a new user and authentication, and seemed to have noticed that when you create a new user in Laravel, no Auth object is created - which you use to get the (new) logged-in user's ID in the rest of the application. This is a problem, and I believe what you may be asking. I did this kind of cludgy hack in userController::store :

$user->save();
	
Session::flash('message','Successfully created user!');
	
//KLUDGE!! rest of site depends on user id in auth object - need to force/create it here
Auth::attempt(array('email' => Input::get('email'), 'password' => Input::get('password')), true);
	
Redirect::to('users/' . Auth::user()->id);

Shouldn't have to create and authenticate, but I didn't know what else to do.

Solution 16 - Php

The first check user logged in and then

if (Auth::check()){
    //get id of logged in user
    {{ Auth::getUser()->id}}

    //get the name of logged in user
    {{ Auth::getUser()->name }}
}

Solution 17 - Php

For Laravel 6.X you can do the following:

$user = Auth::guard(<GUARD_NAME>)->user();
$user_id = $user->id;
$full_name = $user->full_name;

Solution 18 - Php

 if(Auth::check() && Auth::user()->role->id == 2){ 
         $tags = Tag::latest()->get();
        return view('admin.tag.index',compact('tags'));
    }

Solution 19 - Php

You can try :

$id = \Auth::user()->id

Solution 20 - Php

You can try this for laravel 7 :

{{ Auth::user()->name  }}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoshView Question on Stackoverflow
Solution 1 - PhpDr. MAFView Answer on Stackoverflow
Solution 2 - PhpAltrimView Answer on Stackoverflow
Solution 3 - Phpjody_lognoulView Answer on Stackoverflow
Solution 4 - PhpmaztchView Answer on Stackoverflow
Solution 5 - Phpfrankfurt-laravelView Answer on Stackoverflow
Solution 6 - Phpuser761100View Answer on Stackoverflow
Solution 7 - PhprashedcsView Answer on Stackoverflow
Solution 8 - PhpAshwani PanwarView Answer on Stackoverflow
Solution 9 - PhpParveen ChauhanView Answer on Stackoverflow
Solution 10 - PhpMd Sifatul IslamView Answer on Stackoverflow
Solution 11 - Phpl3okorView Answer on Stackoverflow
Solution 12 - PhpVahid AlvandiView Answer on Stackoverflow
Solution 13 - PhpAbd AbughazalehView Answer on Stackoverflow
Solution 14 - PhpOmar TariqView Answer on Stackoverflow
Solution 15 - PhpAndrew KoperView Answer on Stackoverflow
Solution 16 - PhpAbid ShahView Answer on Stackoverflow
Solution 17 - PhpAjjay AroraView Answer on Stackoverflow
Solution 18 - PhpMd.Azizur RahmanView Answer on Stackoverflow
Solution 19 - PhprashedcsView Answer on Stackoverflow
Solution 20 - PhpmrhmtView Answer on Stackoverflow