Laravel v5.2.* (v5.2.29) Auth::guard('api')->attempt($user) 致命错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36534682/
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
Laravel v5.2.* (v5.2.29) Auth::guard('api')->attempt($user) fatal error
提问by Vanya Avchyan
Has anyone encountered this problem
有没有人遇到过这个问题
dd($this->user->check()); return false
but
但
Auth::guard('user')->attempt(App\User::find(1))
return the error
返回错误
Call to undefined method Illuminate\Auth\TokenGuard::attempt()
调用未定义的方法 Illuminate\Auth\TokenGuard::attempt()
Please help to resolve this problem.
请帮助解决这个问题。
回答by Vanya Avchyan
I solved this problem
我解决了这个问题
In config/auth.php
configuration:
在config/auth.php
配置中:
'user' => [
'driver' => 'token',
'provider' => 'userProvider',
],
We need to change to:
我们需要改为:
'user' => [
'driver' => 'session',
'provider' => 'userProvider',
],
Because Auth::guard
data is stored in the session
因为Auth::guard
数据存储在会话中
And further work on the well-known scheme
以及对众所周知的方案的进一步工作
Auth::login() = Auth::guard('user')->login()
Auth::attempt() = Auth::guard('user')->attempt()
Auth::user() = Auth::guard('user')->user()
...
回答by MikielD.
To the function attempt
you shall pass an array of creditianals, not a user object.
attempt
您应该向该函数传递一个creditianals 数组,而不是一个用户对象。
For example:
例如:
Auth::guard('user')->attempt([
'email' => '[email protected]',
'password' => '1234'
]);
And if you are using Laravel and need to authenticate the user, you can call similar:
如果您正在使用 Laravel 并且需要对用户进行身份验证,您可以调用类似的方法:
auth()->attempt([
'email' => '[email protected]',
'password' => '1234'
]);