删除 laravel 通行证用户令牌

Remove a laravel passport user token

在我的单元测试中,我为一个用户生成了令牌:

$tokenString = $this->user->createToken('PHPunit', ['example'])->accessToken;

之后如何删除该用户的令牌?

我认为这样的事情可以撤销令牌:

$this->user->token()->revoke()

基于this link.

这是我在用户注销时所做的。

public function logout() {
    Auth::user()->tokens->each(function($token, $key) {
        $token->delete();
    });

    return response()->json('Successfully logged out');
}

此代码将删除用户生成的每个令牌。

最好的解决方案是这个

public function logout(LogoutRequest $request): \Illuminate\Http\JsonResponse
    {
        if(!$user = User::where('uuid',$request->uuid)->first())
            return $this->failResponse("User not found!", 401);

        try {
            $this->revokeTokens($user->tokens);

            return $this->successResponse([

            ], HTTP_OK, 'Successfully Logout');

        }catch (\Exception $exception) {
            ExceptionLog::exception($exception);

            return $this->failResponse($exception->getMessage());
        }

    }

    public function revokeTokens($userTokens)
    {
        foreach($userTokens as $token) {
            $token->revoke();
        }
    }