Laravel 认证中如何处理自定义列名、用户名和密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21498739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle custom names of columns, username and password in laravel authentication
提问by manuthalasseril
I have a table called logindetials
with the following columns
我有一个名为logindetials
以下列的表
Login_Id | Login_Name | Login_Passwor | Login_Email | .......
When I try to authenticate , It results an error, which is given below
当我尝试进行身份验证时,会导致错误,如下所示
Illuminate \ Database \ QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'username' in 'where
clause' (SQL: select * from `logindetails` where `username` = admin limit 1)
I had tried these in config/auth.php
我曾在 config/auth.php 中尝试过这些
'table' => 'logindetails',
'username' => 'Login_Name',
'password' => 'Login_Passwor',
and in models/User.php
并在模型/User.php
protected $fillable=array('Login_Name','Login_Passwor','Login_Email',...);
protected $primaryKey = "Login_Id";
protected $table = 'logindetails';
protected $hidden = array('Login_Passwor');
public function getAuthIdentifier()
{
return $this->getKey();
}
public function getAuthPassword()
{
return $this->Login_Passwor;
}
public function getReminderEmail()
{
return $this->Login_Email;
}
and also in login controller
并且也在登录控制器中
if (Input::server("REQUEST_METHOD") == "POST") {
$validator=Validator::make
(Input::all(), ["userid" => "required","password" => "required"]);
if ($validator->passes()) {
$credentials = [
"Login_Name" => Input::get("userid"),
"Login_Passwor" => Input::get("password")];
if (Auth::attempt($credentials)) {
return Redirect::route("/");
}
}
$data["errors"] = new MessageBag(["password" =>
["Username and/or password invalid."]]);
$data["Login_Name"] = Input::get("userid");
return Redirect::route("login")->withInput($data);
}
But no raksha. Please somebody help me out from this
但没有罗刹。请有人帮我解决这个问题
回答by nitish.kasar
Actually There is otherway around using which you can keep custom password field.
实际上还有其他方法可以使用您可以保留自定义密码字段。
Just Do Following :
只需执行以下操作:
Pretending that you want custom password password as 'Login_Passwor'. For this just add one more function in models\User.php as follows
假装您想要自定义密码密码为“Login_Passwor”。为此,只需在 models\User.php 中再添加一个函数,如下所示
public function getAuthPassword()
{
return $this->Login_Passwor;
}
Now in your login controller just make this change for Auth:attempt
现在在您的登录控制器中对 Auth:attempt 进行此更改
$credentials = ["Login_Name" => Input::get("userid"),
"password" => Input::get("password")];
if (Auth::attempt($credentials))
{
return Redirect::route("/");
}
This method will work for sure , have tried it
这个方法肯定有效,试过了
回答by Xethron
When authenticating, you need to change username to Login_Name, however, password needs to stay password:
认证的时候需要把username改成Login_Name,password还是password:
Auth::attempt( array('Login_Name' => $username, 'password' => $password) )
Auth::attempt( array('Login_Name' => $username, 'password' => $password) )
Take a look at this file for better understanding of how the User is retrieved when Authenticating: https://github.com/illuminate/auth/blob/master/EloquentUserProvider.php#L60
查看此文件以更好地了解身份验证时如何检索用户:https: //github.com/illuminate/auth/blob/master/EloquentUserProvider.php#L60