2017-02-10 198 views
1

我试图用这个验证用户 - Laravel

//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' 
      ]); 
     } 

验证登录时使用,但我得到这个错误..

QueryException在Connection.php行647: SQLSTATE在 'where子句' 1054未知列 '$ 2Y $ 10 $ 5ysCvqUiloxmtRo2comd9uaiaNkLJ0eiW6x5pDFGWESAbXr5jm5N6'[42S22]::柱未找到(SQL:SELECT * FROM users其中email为空并$2y$10$5ysCvqUiloxmtRo2comd9uaiaNkLJ0eiW6x5pDFGWESAbXr5jm5N6为空极限1)

可以请有人帮我这个。 谢谢:)

回答

2

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

如果喜欢你做:

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

它将产生这种阵列:

['email' => '[email protected]', 's9audh87h2ueahjdbas' => null] 

request()方法需要与它代表的请求中提供的输入的名称值的数组。所以如果你给它一个关联数组,它将完全忽略这些关键字,它只会获取它的值。

因此,要获得来自请求的输入,并在授权尝试使用它,你必须这样做:

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

这是事实假设你的user表有一个emailpassword列。

+1

非常感谢。 – Martina

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)我不知道你是什么传递作为参数。某些类型的混合阵列

+0

这不起作用。现在只是给我错误“请检查你的电子邮件或密码”,即使它们是正确的。密码是用bcrypt创建的。 – Martina

+0

你如何保存密码?Laravel也使用bcrypt,所以这不应该是一个问题。疯狂的猜测说你可能会在保存密码时加密两次。 – devk