Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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中进行自定义身份验证查询_Php_Laravel - Fatal编程技术网

Php 如何在Laravel中进行自定义身份验证查询

Php 如何在Laravel中进行自定义身份验证查询,php,laravel,Php,Laravel,我刚开始使用Laravel,我有一些问题。使用以下命令创建身份验证时: php artisan make:auth 它会自动创建身份验证控制器、视图和默认用户表。 在这里,我想做一些不同的验证。我有一张这样的桌子: 员工 id | reg_number | name id | name | password | remember_token | employee_id | created_at | updated_at 用户 id | reg_number | name id | nam

我刚开始使用Laravel,我有一些问题。使用以下命令创建身份验证时:

php artisan make:auth
它会自动创建身份验证控制器、视图和默认用户表。 在这里,我想做一些不同的验证。我有一张这样的桌子:

员工

id | reg_number | name
id | name | password | remember_token | employee_id | created_at | updated_at
用户

id | reg_number | name
id | name | password | remember_token | employee_id | created_at | updated_at
当我登录时,我想加入
employees
users
表,因此查询如下:

select users.*, employees.reg_number inner join employees on employees.id = users.employee_id
然后,我想使用
reg\u号码
密码
登录

我怎样才能创造出这样的东西


我已经在很多网站上搜索过了,但没有找到任何解决方案。提前感谢您的回答。

您正在尝试使用两个不同的表和两个不同的模型进行身份验证,因此通常的
Auth::trunt()
可能无法正常工作。但是,您可以自己手动验证用户。是否存在任何安全风险/问题?我不知道。与专家们一起看

因此,您要做的是,当用户使用其
reg\u号码登录时。是吗

public function authenticate(Request $request)
{
    $employee = Employee::where('reg_number', $request->reg_number)->first();

    if ( !$employee ) { //If no employee was found
        return redirect()->back()->withErrors(['reg_number' => 'No such reg_number ']);
    }

    //Assuming you have the relationship setup between employee and user
    //probably a one to one. 
    $user = $employee->user;

    //manually check their password
    //assuming you hash every password when saving the user 
    if (Hash::check($request->password, $user->password)) {
        //password matched. Log in the user 
        Auth::login($user);
        return redirect()->intended('dashboard');
    }

    return redirect()->back()->withErrors(['credentials' => 'Check your credentials']);
}

当凭据为真时,如何重定向到下一个请求?如果有人试图访问某个页面并被重定向到登录,登录后您可以执行
return redirect()->designed('dashboard')。如果他们只是简单地进入登录,那么designed将把他们带到他们想去的地方,或者默认带到仪表板。