Reputation: 6139
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.
Upvotes: 140
Views: 656069
Reputation: 11
If you are using Laravel 11 or later, you can use
$id =Auth::id();
instead of $id = Auth::user()->id;
and be sure to make:
use Illuminate\Support\Facades\Auth;
Upvotes: 1
Reputation: 5776
In laravel 10:
$id = auth()->id();
Reference:
vendor\laravel\framework\src\Illuminate\Contracts\Auth\Guard.php
/**
* Get the ID for the currently authenticated user.
*
* @return int|string|null
*/
public function id();
Upvotes: 1
Reputation: 4151
2023's here. If the session is over and you are trying to reload old page it will still show that stuff . The only good solution I found out so far is to screw in a tail middleware('auth') to your route in web.php Say:
Route::get('/my_admin/{userid}', 'my\DearRoute@giveMeMyDarlingView')
->name('comeup_dear')->middleware('auth');
and now if in your browser lasts some out sessioned say https://myserver.com/my_admin/6 leavings and you are trying to refresh page it will kick you out to login page.
Edit: or if there are a lot of such routes embrace them into:
Route::middleware(['auth'])->group(function () {
...
Route::get('/my_admin/{userid}', my\DearRoute@giveMeMyDarlingView')->name('comeup_dear')
...
});
still there in web.php
And you are done
Upvotes: 0
Reputation: 1306
As highlighted in the above feedback ensure that first and foremost is that you have defined the Auth Facade in your controller. Depending on the version of your laravel App the declaration of the Auth would look something like this :
5.8 and below
use Illuminate\Support\Facades\Auth;
version 6 and above
use Auth;
Also in your routes file ensure that the auth middleware encloses the route.
For a single endpoint try (Code sample for laravel 8)
Route::post('update-profile', [UserController::class, 'profileUpdate'])->middleware('auth:sanctum');
For multiple endpoints
Route::middleware(['auth:sanctum'])->group(function () {
Route::post('update-profile', [UserController::class, 'profileUpdate']);
}
Upvotes: 0
Reputation: 7724
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
Upvotes: 3
Reputation: 2317
In Laravel 8.6, the following works for me in a controller:
$id = auth()->user()->id;
Upvotes: 22
Reputation: 5535
It's working with me :
echo Auth::guard('guard_name')->user()->id;
Upvotes: 4
Reputation: 1496
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();
Upvotes: 10
Reputation: 375
if(Auth::check() && Auth::user()->role->id == 2){
$tags = Tag::latest()->get();
return view('admin.tag.index',compact('tags'));
}
Upvotes: 3
Reputation: 3725
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;
Upvotes: 11
Reputation: 154
For Laravel 6.X you can do the following:
$user = Auth::guard(<GUARD_NAME>)->user();
$user_id = $user->id;
$full_name = $user->full_name;
Upvotes: 3
Reputation: 475
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 }}
}
Upvotes: 3
Reputation: 612
you must check is user loggined ?
Auth::check() ? Auth::user()->id : null
Upvotes: 4
Reputation: 4131
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.
Upvotes: 15
Reputation: 1985
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
Upvotes: 123
Reputation: 854
If anyone is looking for laravel 5 >
Auth::id()
give id of the authorized user
Upvotes: 7
Reputation: 817
Do a Auth::check() before to be sure that you are well logged in :
if (Auth::check())
{
// The user is logged in...
}
Upvotes: 53
Reputation: 7191
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.
Upvotes: 3
Reputation: 71
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..
Upvotes: 7
Reputation: 4568
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 () {});
.
Upvotes: 9
Reputation: 1697
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();
}
Upvotes: 25
Reputation: 6756
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
.
Upvotes: 73