首先感谢阅读这篇文章!
我对Laravel很新,我在validateLogin
上压倒了LoginController
方法。我在isActivated
下调用用户模型中的app\User
方法,但我得到以下错误,我甚至硬编码它返回true进行测试。我确定我做了一些蠢事,有什么建议吗?
Call to a member function isActivated() on null
应用程序\ HTTP \ \控制器验证\ LoginController.php
protected function validateLogin(Request $request)
{
//if(User::isActivated()->isActivated() == false)
if (Auth::user()->isActivated())
{
return redirect('/contact');
}
应用程序\ user.php的
public function isActivated()
{
//return $this->confirmed ;
return true;
}
你在isActivated()
上调用null
,因为没有经过身份验证的用户。要使代码生效,您需要首先检查用户是否已经过身份验证,然后是否已激活。
if (Auth::user() && Auth::user()->isActivated()) {
// Safely check that a user is authenticated and activated.
}
如果您关注本指南:https://laravel.com/docs/5.5/authentication#authenticating-users
你可以使用Auth::attempt
方法。
if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
// The user is active, not suspended, and exists.
}