我正在尝试创建一个登录页面,以检查密码。此方法前一段时间有效,但现在却出现以下错误:
Trying to get property 'password' of non-object
问题在于检索和检查散列密码。但是,我找不到错误。有人可以帮我吗。我的代码如下:
public function logs_in(Request $request){
$email = $request->input('email');
$password = $request->input('password');
$hashedPassword = User::where('email', $email)->first();
if(Hash::check($password, $hashedPassword->password)){
$request->session()->put('success');
return redirect()->route('admin');
} else {
return redirect()->route('login')->with('login_error', 'Invalid
credentials entered');
}
}
这意味着数据库中没有用户使用给定的电子邮件,并且此查询的结果是null
:
User::where('email', $email)->first()
因此,将代码更改为:
if ($hashedPassword && Hash::check($password, $hashedPassword->password)) {
或使用optional()
助手:
if (Hash::check($password, optional($hashedPassword)->password)) {
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句