[英]Laravel Multiple Auth::attempt
I have two models in my laravel v5.4 project, user and admin. 我的laravel v5.4项目中有两个模型,用户和管理员。
In config/auth.php i added admin to guards and providers as below : 在config / auth.php中,我将admin添加到警卫和提供者,如下所示:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class,
],
],
Now in AdminController class i want to use Auth::attempt function but by default it uses the users table. 现在在AdminController类中,我想使用Auth :: attempt函数,但默认情况下它使用users表。 I can change defaults in config/auth.php as below and it works but in this case i can not use Auth::attempt for users.
我可以更改config / auth.php中的默认值,如下所示它可以工作,但在这种情况下我不能使用Auth :: attempt for users。
'defaults' => [
'guard' => 'admin',
'passwords' => 'users',
],
I want to set user as default but use Auth::attempt function for admin with a method like Auth::attempt('admin',[credentials]). 我想将用户设置为默认值,但使用Auth :: attempt('admin',[credentials])等方法为admin使用Auth :: attempt函数。 How can i use Auth::attempt for Admin model?
我如何使用Auth :: attempt for Admin模型?
You call the guard directly, like this: 你直接打电话给警卫,像这样:
Auth::guard('admin')->attempt($credentials);
Or with the helper: 或者帮助者:
auth()->guard('admin')->attempt($credentials);
Or, even shorter with the helper: 或者,帮助者更短:
auth('admin')->attempt($credentials);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.