Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5双向表登录_Php_Mysql_Laravel_Model View Controller_Laravel 5 - Fatal编程技术网

Php Laravel 5双向表登录

Php Laravel 5双向表登录,php,mysql,laravel,model-view-controller,laravel-5,Php,Mysql,Laravel,Model View Controller,Laravel 5,我正在尝试配置一个laravel 5应用程序,该应用程序将根据用户的用户名和密码登录用户,如果用户名和密码方法失败,应用程序将尝试检查另一个表,如果用户名变量中的数据在我的情况下是可用的,那么他们的雇员id。因此,下面是我使用传统的laravel方法登录用户的代码: $authdetails = array( 'email' => $request->input('email'), 'password' => $request-

我正在尝试配置一个laravel 5应用程序,该应用程序将根据用户的用户名和密码登录用户,如果用户名和密码方法失败,应用程序将尝试检查另一个表,如果用户名变量中的数据在我的情况下是可用的,那么他们的雇员id。因此,下面是我使用传统的laravel方法登录用户的代码:

 $authdetails = array(
            'email' => $request->input('email'),
            'password' => $request->input('password')
         );
         if (Auth::attempt($authdetails)) {
             // Authentication passed...
             return redirect()->to('/home');
         }
        else {
              return back()->with('error', 'Invalid username or password')->withInput();
          }
     }
数据库中有两个表。(1) 用户,(2)userdetails,用户表包含标准的laravel用户默认迁移表,userdetails表包含用户的详细信息。登录身份验证的第一种方法使用users表,如果第一种方法失败,我想实现的第二种登录方法使用userdetails表


我试图做的是,如果登录的第一个方法失败,它将在另一个表中搜索传递的变量的值(如果有),然后它将对其进行身份验证。

如果您想在第一个表身份验证失败时使用两个不同的表登录。您必须使用multiauth库和

然后像这样使用

$authdetails = array(
        'email' => $request->input('email'),
        'password' => $request->input('password')
     );
     if (Auth::user1()->attempt($authdetails)) {
         // Authentication passed...
         return redirect()->to('/home1');
     }
     if (Auth::user2()->attempt($authdetails)) {
         // Authentication passed...
         return redirect()->to('/home2');
     }
    else {
          return back()->with('error', 'Invalid username or password')->withInput();
      }
 }

如果在用户表中有
employee\u id
列,那么就很容易了。您只需尝试使用这两组凭据进行身份验证

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

    // Attempt login via username
    $credentials = [
        'username' => $request->input('usernameOrEmployeeId'),
        'password' => $request->input('password'),
    ];
    if (Auth::attempt($credentials, $request->has('remember'))) {
        return redirect()->intended($this->redirectPath());
    }

    // Swap username and employee_id
    $credentials['employee_id'] = $request->input('usernameOrEmployeeId');
    unset($credentials['username']);

    // Attempt login via employee_id
    if (Auth::attempt($credentials, $request->has('remember'))) {
        return redirect()->intended($this->redirectPath());
    }

    // Login attempts failed
    return redirect($this->loginPath())
        ->withInput($request->only('usernameOrEmployeeId', 'remember'))
        ->withErrors([
            'usernameOrEmployeeId' => $this->getFailedLoginMessage(),
        ]);
}
如果在用户表中没有
employee\u id
列,那么就有点困难了。您将使用与上述相同的代码,除非您将替换此部件:

// Swap username and employee_id
$credentials['employee_id'] = $credentials['username'];
unset($credentials['username']);
比如说:

// Find user_id by looking up the employee relationship
$employee = App\Employee::findOrFail($request->input('usernameOrEmployeeId'));
$credentials['id'] = $employee->user->id;
unset($credentials['username']);
不要忘记
unset()
。这一点很重要,因为如果您将用户名保留在数组中,Laravel将尝试使用所有3条信息进行身份验证,并且将始终失败,因为我们已经知道用户名/密码不匹配


此外,您不必使用任何第三方库。这是一个相对简单的解决方案的简单问题。

您的模式是什么样子的?
employee\u id
是用户表中的一列吗?用户和员工的关系如何?编辑的问题。谢谢测试和工作。虽然这是很多硬编码要做。谢谢