Laravel

Laravel sanctumのログアウトでエラー

Method Illuminate\Auth\RequestGuard::logout does not exist. {"userId":"xxxxxxxxxxxx","exception":"[object] (BadMethodCallException(code: 0): Method Illuminate\Auth\RequestGuard::logout does not exist. at /app/vendor/laravel/framework/src/Illuminate/Macroable/Traits/Macroable.php:113)

というエラーが出ました。

https://laravel.com/docs/9.x/authentication#logging-out
を参考に Auth::logout(); をしていたがsanctumの場合は違う様子。

Auth::guard('sanctum')->user()->tokens()->delete();

とすることで解決できました。
具体的にはControllerにて下記のようにします。

   public function logout(Request $request): JsonResponse {
      Auth::guard('sanctum')->user()->tokens()->delete();
      $res = [];
      return response()->json($res, Response::HTTP_OK);
   }

なおAuth::guard('web')->logout();を解決法としている記事がいくつかありました。
しかしながらこれ、エラーはでませんが確認したらトークンは有効なままで、何もしないのと同じで意味はありませんでした。

参考:https://www.messiahworks.com/archives/23816

-Laravel