Question: I get this error when i am trying to logout the user

```
  public function logout(Request $request)
    {
       $request->user()->tokens()->delete();
    }

```

[![enter image description here](https://i.stack.imgur.com/09T6r.png)](https://i.stack.imgur.com/09T6r.png)

Answer: 
This worked for me, Change this code in your `Controller.php` to,

```
public function logout(Request $request){
    Auth::user()->tokens()->delete();
    return [
        'message' => 'logged out'
    ];
}

```

And make sure your Route POST request is protected in `api.php`, change the code to below

```
Route::post('/logout', [AuthController::class, 'logout'])->middleware('auth:sanctum');

```
use `token()` instead of `tokens()`

```
$request->user()->token()->delete();

```

Or you can use it as below.

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

```

by using revoke

```
$user = $request->user();

foreach ($user->tokens as $token) {
    $token->revoke();
}

```