FatalThrowableError:参数1传递了.. :: fromUser()Laravel 5.6

时间:2019-02-08 13:53:02

标签: php laravel jwt laravel-5.6

我使用邮递员添加用户或登录,用户添加成功,但出现此错误

  

“传递给Tymon \ JWTAuth \ JWT :: fromUser()的参数1必须是   Tymon \ JWTAuth \ Contracts \ JWTSubject的实例,App \ User的实例   给定,在C:\ Users \ Web中调用   工作站\桌面\ laravelapp \ jwtlaravel \供应商\ 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'));


    }
}

2 个答案:

答案 0 :(得分:1)

在用户模型中实施JWTSubject

class User extends Authenticatable implements JWTSubject

答案 1 :(得分:0)

这要求 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 [];
    }
}