How to handle custom names of columns, username and password in laravel authentication

I have a table called logindetials with the following columns

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

'table' => 'logindetails',
'username' => 'Login_Name',
'password' => 'Login_Passwor', 

and in models/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

Answers


When authenticating, you need to change username to Login_Name, however, password needs to stay 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


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

public function getAuthPassword() 
{
   return $this->Login_Passwor; 
}

Now in your login controller just make this change for 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


Need Your Help

Match up database record with image from upload folder

php mysql database image image-uploading

I have a database with users information such as name, email etc in. When they sign up they must upload a picture. At present when they do. the image goes into a 'uploads' folder on the server.

How do I install an .egg file without easy_install in Windows?

python easy-install egg

I have Python 2.6 and I want to install easy _ install module. The problem is that the only available installation package of easy _ install for Python 2.6 is an .egg file! What should I do?