调用未定义的方法Illuminate \ Auth \ TokenGuard :: attempt()

时间:2017-09-01 15:12:36

标签: php laravel

我收到了这个错误:

  

调用未定义的方法Illuminate \ Auth \ TokenGuard :: attempt()

从这段代码:

if(Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->passsword], $request->remember)){
            return redirect()->intended(route('admin.dashboard'));
        }else{

我已导入Illuminate\Support\Facades\Auth as the docs suggest

我的auth.php可能有帮助

<?php

return [

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
        'admin-api' => [
            'driver' => 'token',
            'provider' => 'admins',
        ],
    ],
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],
    ],
    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'admins' => [
            'provider' => 'admins',
            'table' => 'password_resets',
            'expire' => 15,
        ],
    ],
];

2 个答案:

答案 0 :(得分:3)

我认为您可能尝试使用“会话”防护驱动程序而不是“令牌”。所以,试着这样做:

config / auth.php 配置文件中:

'admin-api' => [
    'driver' => 'token',
    'provider' => 'admins',
],

您必须将其更改为

'admin-api' => [
    'driver' => 'session',
    'provider' => 'admins',
],

然后你应该运行:

  • php artisan cache:clear
  • php artisan config:cache

再试一次。祝你好运!

答案 1 :(得分:2)

令牌保护没有尝试方法,这是会话身份验证中使用的功能。因此,您需要自己授权用户或使用Laravel Passport认证https://laravel.com/docs/6.x/passport