2015-09-25 118 views
0

在登錄時,查詢失敗,因爲「電子郵件」是不是「usuario」,它在「人物」更改SQL查詢,Laravel 5.1

Unknown column 'email' in 'where clause' (SQL: select * from `usuario` where `email` = [email protected] limit 1) 

這不是改變數據庫的解決方案模型,因爲不是所有的「角色」是「usuario」,但所有的「usuario」是「人物」。

試圖設置的關係:

class Persona extends Model implements AuthenticatableContract, 
           AuthorizableContract, 
           CanResetPasswordContract 
{....} 
public function usuario() 
{ 
    return $this->hasOne('App\Usuario'); 
} 
//----------------------------------------------------// 
class Usuario extends Model implements AuthenticatableContract, 
           AuthorizableContract, 
           CanResetPasswordContract 
{ 
{....} 
public function persona() 
{ 
    return $this->hasOne('App\Persona'); 
} 

兩個表都有相同的密鑰。

但查詢不會改變,我雖然也許Laravel可以在某處做一個「INNER JOIN」,不知道Laravel是否可以自動執行此操作,所以我嘗試更改查詢但不知道確切位置位於。

我覺得在這樣一個解決方案,但它看起來太容易了,不知道是否會是一個很好的方式=/


  • 獲取電子郵件和passwd從後
  • 獲取ID,電子郵件和PASSWD從BD與SQL
  • 如果[EMAIL和passwd匹配]驗證:: loginUsingId(ID); [ELSE]返回與錯誤。

據我所知,驗證:: loginUsingId(ID);就像一個成功的驗證::嘗試()...但這個解決方案,我需要知道以後該怎麼實現油門和「記住」單獨選項...所有的心思都歡迎:d

回答

0

我發現一個解決方案:改變postLogin()但AuthController裏面,所以我可以保留Throttles和Remember功能,而且核心仍然沒有改變,這裏是代碼,如果我可以幫助其他人:

//------------------------------------ 
// Auth\AuthController.php 
//------------------------------------ 

protected function postLogin(Request $request) 
{ 
    $this->validate($request, [ 
     $this->loginUsername() => 'required', 'password' => 'required', 
    ]); 

    // If the class is using the ThrottlesLogins trait, we can automatically throttle 
    // the login attempts for this application. We'll key this by the username and 
    // the IP address of the client making these requests into this application. 
    $throttles = $this->isUsingThrottlesLoginsTrait(); 

    if ($throttles && $this->hasTooManyLoginAttempts($request)) { 
     return $this->sendLockoutResponse($request); 
    } 

    $credentials = $this->getCredentials($request); 

    //Here's the custom SQL, so you can retrieve a "user" and "pass" from anywhere in the DB 
    $usuario = \DB::select(' 
      SELECT 
       persona.nombre, 
       usuario.password 
      FROM 
       persona 
      INNER JOIN 
       usuario ON persona.id_persona = usuario.id_persona 
      WHERE 
       persona.email = ? 
      LIMIT 1', array($credentials['email'])); 

    // Instead of: 
    // if (Auth::attempt($credentials, $request->has('remember'))) { 
    if ($usuario && Hash::check($credentials['password'], $usuario[0]->password)) { 
     Auth::loginUsingId($usuario[0]->id_persona, $request->has('remember')); 

     // Put any custom data you need for the user/session 
     Session::put('nombre', $usuario[0]->nombre); 

     return $this->handleUserWasAuthenticated($request, $throttles); 
    } 

    // If the login attempt was unsuccessful we will increment the number of attempts 
    // to login and redirect the user back to the login form. Of course, when this 
    // user surpasses their maximum number of attempts they will get locked out. 
    if ($throttles) { 
     $this->incrementLoginAttempts($request); 
    } 

    return redirect($this->loginPath()) 
     ->withInput($request->only($this->loginUsername(), 'remember')) 
     ->withErrors([ 
      $this->loginUsername() => $this->getFailedLoginMessage(), 
     ]); 
}