0

我在 py 项目中使用 laravel 护照,我想在每个请求中创建一个令牌以使其安全,但它现在不起作用,我真的很困惑我的代码有什么问题,请帮助我。

这是我的模型


use App\Models\Post;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\Models\Media;
use Laravel\Passport\HasApiTokens;


class User extends Authenticatable implements HasMedia
{
    use HasApiTokens, Notifiable;

    use HasMediaTrait;


    public function registerMediaConversions(Media $media = null)
    {

        $this->addMediaConversion('thumb')
            ->crop('crop-center', 50, 50);


        $this->addMediaConversion('list')
            ->fit('crop', 312, 312);


        $this->addMediaConversion('big')
            ->fit('fill', 1248, 1248);

    }

    protected $fillable = [
        'name', 'email', 'password', "role"
    ];
    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];


    public function posts()
    {

        return $this->hasMany(Post::class, 'post_author');

    }


}

这是我的控制器

    {
        $data = \App\User::all();
        $accessToken = $data->createToken('Token')->accessToken;
        return response(['usersData' => $data]);
    }
4

1 回答 1

0

我用下面的代码解决了我的问题:)

 $user = Auth::user();
        $success['token'] =  $user->createToken('MyApp')-> accessToken;
        return response()->json(['success' => $success], $this-> successStatus);
于 2020-09-23T05:55:24.513 回答