在登录时验证用户 - Laravel

时间:2017-02-10 00:40:35

标签: php laravel authentication

我正在尝试使用此

验证登录时的使用情况
 //Attempt to auth the user
        if(! auth()->attempt(request(['email','password' => Hash::make(request('password'))])))                            
        {

            //If not redirect back
            return back()->withErrors([

                'message' => 'Please check your email or password'
            ]);
        }

但是我收到了这个错误..

  

Connection.php第647行中的QueryException:   SQLSTATE [42S22]:未找到列:1054未知列&$; $ 2y $ 10 $ 5ysCvqUiloxmtRo2comd9uaiaNkLJ0eiW6x5pDFGWESAbXr5jm5N6'在' where子句' (SQL:select * from users其中email为空,$2y$10$5ysCvqUiloxmtRo2comd9uaiaNkLJ0eiW6x5pDFGWESAbXr5jm5N6为空限制1)

请有人帮助我。 谢谢:))

2 个答案:

答案 0 :(得分:2)

发生此错误是因为您滥用request()辅助方法。

如果你愿意,你做了:

request(['email','password' => Hash::make(request('password'))])

它将产生这个数组:

['email' => 'myemail@lol.com', 's9audh87h2ueahjdbas' => null]

request()方法需要一个数组,其值表示请求中提供的输入的名称。因此,如果你给它一个关联数组,它将完全忽略键,它只会获取它的值。

因此,要获取请求的输入并在授权尝试中使用它,您必须这样做:

$userInput = request('email', 'password');
if (! auth()->attempt($userInput))
{
  //do something.
}

假设您的user表格中有emailpassword列,这是正确的。

答案 1 :(得分:0)

试试这个:

//Attempt to auth the user
if(! auth()->attempt([
        'email' => request()->get('email'),
        'password' => request()->get('password')
    ])                            
{

    //If not redirect back
    return back()->withErrors([

        'message' => 'Please check your email or password'
    ]);
}

a)您不需要哈希密码,Laravel已经这样做了

b)我不确定你作为参数传递了什么。某种类型的混合数组

相关问题