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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46003253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:35:20  来源:igfitidea点击:

Call to undefined method Illuminate\Auth\TokenGuard::attempt()

phplaravel

提问by Ricky Orlando Napitupulu

I'm getting this error:

我收到此错误:

Call to undefined method Illuminate\Auth\TokenGuard::attempt()

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

From this code:

从这个代码:

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

I have imported Illuminate\Support\Facades\Authas the docs suggest

我已按照文档建议导入Illuminate\Support\Facades\Auth

My auth.phpmay help

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,
        ],
    ],
];

回答by Felipe Barragán

I think you are probably trying to use a "session" guard driver instead "token". So, try to do this:

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

In config/auth.phpconfiguration file:

config/auth.php配置文件中:

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

You have to change it to

你必须把它改成

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

Then you should run:

然后你应该运行:

 php artisan cache:clear
 php artisan config:cache

And give it a try again. Good luck!

再试一次。祝你好运!

回答by Darkshifty

The token guard doesn't have an attempt method, this is a function used in session authentication. So you will need to authorize the users yourself or use the Laravel Passport authentication https://laravel.com/docs/6.x/passport

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