Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Rizwan5019's avatar

Laravel REST API with sanctum Authentication | Delete current user token

I am working on CRUD rest api in laravel. I am facing a issue on Logout function, when i tried auth()->user()->tokens()->delete(); It showing error on tokens(). I have added HasApiTokens in User Model but it still not working for me. Can any one help me?. Thanks in advance.

0 likes
4 replies
LetherMade's avatar

Hi @rizwan5019 , Can you kindly provide the error? That should give us more information what maybe is going wrong

Rizwan5019's avatar

@LetherMade annotation line tell PHP intelephense that Undefined method 'tokens'. I am using Hasapitokens in my User Model.

tetranyble's avatar

@Rizwan5019 it is important to always add the guard to avoid confusion like so: $request->user('sanctum')->currentAccessToken()->delete();

Please or to participate in this conversation.