Having problem understanding methods in Laravel

bennyboy posted 2 months ago


I am a bit lost on this. I created a logout function with the passport API and would like to get the user token during the request.

I went here:

I cannot see what I find everywhere online which is: Auth::user()->accessTokens();

Everyone shows in numerous posts that you can get all the user tokens with it but in the Authenticable page I cannot see this method listed anywhere.

Also, PHP storm warns me of: "Method 'accessTokens' not found in \Illuminate\Contracts\Auth\Authenticatable|null less... (Ctrl+F1) Inspection info: Referenced method is not found in subject class."

I am really confused onto how people managed to find this method and get it to work if it is not found nor listed in the Authenticable laravel page.

Could someone please explain to me how it works?

Thank you.

Cameron replied 2 months ago

What are you trying to do? Use passport inside your application or from an external application?

bennyboy replied 2 months ago

Thank you for the reply. The problem for me is not the logic in itself as I can create a logout and login api, what I am not understanding is where the other devs get this from for Laravel 5.7:


As I cannot find it from here:

I actually cannot find accessTokens() anywhere in the api doc when added to the search bar on that website.

Would you mind pinpointing me to the right direction?

Let's say I have Auth::user()-> , how do I know that accessTokens() is the property used in Laravel 5.7 to get the user access token? I am always relying on posts from others to find these methods but would really like to know how they find them in the first place.


Cameron replied 2 months ago

I’m not sure I have ever heard of it . Where are you seeing people use it ?

bennyboy replied 2 months ago

OK i understand now, it works only when you have a relation in place:

Strange that Laravel does not allow us to retrieve a user token with a default functions created by their team.

bennyboy replied 2 months ago

No need for relations, this guy here explains it:

It is found in the hasApiTokens.php which is in the user model. public function tokens() { return $this->hasMany(Passport::tokenModel(), 'user_id')->orderBy('created_at', 'desc'); }

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us