2015-09-25 117 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(), 
     ]); 
}