Laravel 5.6 multi auth继续在默认模型上进行身份验证

时间:2018-04-11 10:11:13

标签: php laravel laravel-5.6

我有两张桌子登录:考生和公司。公司是我的默认身份验证, 每次我尝试在考生中尝试auth时,它都会继续使用公司的默认表。

我已经看到了几个类似于我的问题,但它似乎对我不起作用,或者我可能忽略了一些你可能有些人可能会看到的东西。

我做错了什么?

Auth.php:

'defaults' => [
    'guard' => 'web',
    'passwords' => 'companies',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'companies',
    ],
    'api' => [
        'driver' => 'token',
        'provider' => 'companies',
    ],
    'examinees' => [
        'driver' => 'session',
        'provider' => 'examinees',
    ],
],

'providers' => [
    'companies' => [
        'driver' => 'eloquent',
        'model' => App\Company::class,
    ],
    'examinees' => [
        'driver' => 'eloquent',
        'model' => App\Examinee::class,
    ],
],

'passwords' => [
    'companies' => [
        'provider' => 'companies',
        'table' => 'password_resets',
        'expire' => 60,
    ],
    'examinees' => [
        'provider' => 'examinees',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],

的LoginController:

public function showLoginForm()
{
    return view('examinee.auth.login');
}

protected function guard()
{
    return Auth::guard('examinees');
}

public function login(Request $request) {
    $user = Examinee::where('email', $request->get('email'))->first();

    if (Auth::attempt(['id' => $user->id, 'password' => $request->get('password')])) {

        // prints data from Company table instead of Examinee...
        echo "AUTH USER:<pre>";
        print_r(Auth::user());
        echo "</pre>";
        // return redirect('/home');
    }
}

型号:

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Examinee extends Authenticatable
{
    use Notifiable;

    protected $table = 'examinees';
    protected $fillable = ['email',  'password'];
    protected $hidden = ['password',  'remember_token'];

    public $timestamps = false;

}

1 个答案:

答案 0 :(得分:0)

我过去如何做到这一点就是将警卫添加到你的身份验证

更改 Auth::attempt(['id' => $user->id, 'password' => $request->get('password')

Auth::guard('examinees')->attempt(['id' => $user->id, 'password' => $request->get('password')