php Laravel:Auth::user()->id 试图获取非对象的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17835886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: Auth::user()->id trying to get a property of a non-object
提问by Josh
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:
当我提交表单以添加用户时,我收到以下错误“试图获取非对象的属性”,错误显然在第一行: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.
有任何想法吗?我已经搜索了一段时间,我看到的一切都表明这应该可以正常工作。我的用户使用 Sentry 2 身份验证包登录。
采纳答案by Altrim
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
.
如果您使用的是Sentry
检查登录用户Sentry::getUser()->id
。您得到的错误是Auth::user()
返回 NULL 并尝试从 NULL 获取 id 因此错误trying to get a property from a non-object
。
回答by Dr. MAF
Now with laravel 4.2 it is easy to get user's id:
现在使用 laravel 4.2 很容易获得用户的 id:
$userId = Auth::id();
that is all.
就这些。
But to retrieve user's data other than id, you use:
但是要检索 id 以外的用户数据,您可以使用:
$email = Auth::user()->email;
For more details, check security part of the documentation
有关更多详细信息,请查看文档的安全部分
回答by jody_lognoul
Do a Auth::check() before to be sure that you are well logged in :
在确保您已正确登录之前执行 Auth::check() :
if (Auth::check())
{
// The user is logged in...
}
回答by maztch
id is protected
, just add a public method in your /models/User.php
id 是protected
,只需在您的/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();
}
回答by frankfurt-laravel
In Laravel 5.6 I use
在 Laravel 5.6 中我使用
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.
似乎适用于旧版本的 Laravel 5.x 并且对我不起作用。
回答by rashedcs
Never forget to include and try to use middleware auth:
永远不要忘记包含并尝试使用中间件身份验证:
use Illuminate\Http\Request;
Then find the id using request method:
然后使用请求方法找到id:
$id= $request->user()->id;
回答by Ashwani Panwar
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 () {});
.
检查您route
正在使用的函数Auth::user()
,为了获取 Auth::user() 数据,函数应该在web
中间件内 Route::group(['middleware' => 'web'], function () {});
。
回答by l3okor
use Illuminate\Support\Facades\Auth;
使用 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..
等等..
回答by Md Sifatul Islam
If anyone is looking for laravel 5 >
如果有人正在寻找 Laravel 5 >
Auth::id()
give id of the authorized user
给出授权用户的id
回答by Vahid Alvandi
you must check is user loggined ?
您必须检查用户是否已登录?
Auth::check() ? Auth::user()->id : null