Laravel:Auth :: user()-> id试图获取非对象的属性 [英] Laravel: Auth::user()->id trying to get a property of a non-object

查看:74
本文介绍了Laravel:Auth :: user()-> id试图获取非对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提交表单以添加用户时,出现以下错误试图获取非对象的属性",该错误显然在第一行:Auth :: user()-> id以下:

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);

有什么想法吗?我搜索了一段时间,看到的所有内容都说这应该工作正常.我的用户使用Sentry 2身份验证捆绑软件登录.

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,请使用Sentry::getUser()->id检查登录的用户.您得到的错误是Auth::user()返回NULL,并且它试图从NULL获取ID,因此错误trying to get a property from a non-object.

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.

这篇关于Laravel:Auth :: user()-> id试图获取非对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆