Laravel在null上调用成员函数isActivated()

问题描述 投票:0回答:2

首先感谢阅读这篇文章!

我对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;
}
php laravel null
2个回答
0
投票

你在isActivated()上调用null,因为没有经过身份验证的用户。要使代码生效,您需要首先检查用户是否已经过身份验证,然后是否已激活。

if (Auth::user() && Auth::user()->isActivated()) {
    // Safely check that a user is authenticated and activated.
}

0
投票

如果您关注本指南: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.
}
© www.soinside.com 2019 - 2024. All rights reserved.