登录时,查询失败,因为“电子邮件”不在“usuario”上,而是在“persona”中
Unknown column 'email' in 'where clause' (SQL: select * from `usuario` where `email` = admin@localhost limit 1)
这不是改变数据库模型的解决方案,因为并非所有“persona”都是“usuario”,但所有“usuario”都是“persona”。
试图设置关系:
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可能会在某个地方做一个“内部加入”,不知道Laravel是否可以自动执行此操作,所以我尝试更改查询但不确切知道在哪里位于。
我想在这样的解决方案中,但它看起来太容易了,不知道是不是一个好方法= /
据我所知,Auth :: loginUsingId(ID);行为就像一个成功的Auth :: attempt()......但是通过这个解决方案,我需要知道如何分别实现后来的Throttles和“记住”选项...欢迎所有的想法:D
答案 0 :(得分: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(),
]);
}