FatalThrowableError:参数 1 已通过 ..::fromUser() laravel 5.6
Posted
技术标签:
【中文标题】FatalThrowableError:参数 1 已通过 ..::fromUser() laravel 5.6【英文标题】:FatalThrowableError:Argument 1 passed ..::fromUser() laravel 5.6 【发布时间】:2021-09-07 20:31:35 【问题描述】:我使用邮递员添加用户或登录,用户添加成功但我收到此错误
"传递给 Tymon\JWTAuth\JWT::fromUser() 的参数 1 必须是 Tymon\JWTAuth\Contracts\JWTSubject 的实例,App\User 的实例 给定,在 C:\Users\Web 中调用 WorkStation\Desktop\laravelapp\jwtlaravel\vendor\tymon\jwt-auth\src\JWTAuth.php 第 54 行"
我在这个 ligne 中找到了这个函数
public function attempt(array $credentials)
if (! $this->auth->byCredentials($credentials))
return false;
return $this->fromUser($this->user());
这是我的用户模型:
<?php
namespace App;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password','username','lastname','tel','tel',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function getJWTIdentifier()
return $this->getKey();
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
return [];
and this my register controller
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\User;
use JWTFactory;
use JWTAuth;
use Validator;
use Response;
class APIRegisterController extends Controller
//
public function register( Request $request)
$validator = Validator::make($request -> all(),[
'email' => 'required|string|email|max:255|unique:users',
'username' =>'required',
'tel' => 'required',
'name' => 'required',
'lastname' => 'required',
'adress' => 'required',
'password'=> 'required'
]);
if ($validator -> fails())
# code...
return response()->json($validator->errors());
User::create([
'name' => $request->get('name'),
'email' => $request->get('email'),
'tel' => $request->get('tel'),
'username' => $request->get('username'),
'lastname' => $request->get('lastname'),
'adress' => $request->get('adress'),
'password'=> bcrypt($request->get('password'))
]);
$user = User::first();
$token = JWTAuth::fromUser($user);
return Response::json( compact('token'));
【问题讨论】:
【参考方案1】:这要求User Model
在你的模型中实现这个契约:
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Model implements JWTSubject
或者
你必须更新你的User model
来实现Tymon\JWTAuth\Contracts\JWTSubject
接口:
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Model implements JWTSubject
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
return $this->getKey();
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
return [];
【讨论】:
【参考方案2】:在您的用户模型中实现JWTSubject
:
class User extends Authenticatable implements JWTSubject
【讨论】:
谢谢我的问题解决了,现在我得到 "token": null 但用户被添加到数据库中?我能做什么? 很高兴它有帮助。你跑php artisan jwt:secret
了吗?以上是关于FatalThrowableError:参数 1 已通过 ..::fromUser() laravel 5.6的主要内容,如果未能解决你的问题,请参考以下文章
Lumen - FatalThrowableError 在 null 上调用成员函数 file()
Laravel 5.6 db:seed 抛出 FatalThrowableError:找不到类 'APP\Todo'
Laravel Tinker 错误:Symfony\Component\Debug\Exception\FatalThrowableError:调用未定义的方法 Psy\Configuration::
Php Symfony\Component\Debug\Exception\FatalThrowableError 解析错误:语法错误,意外',';
lumen.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: 致命错误:调用未定义函数 App\Http\Controller
php artisan optimize NULL.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Call to unde