How to get the user id from laravel passport token?

Member

by jerad , in category: PHP Frameworks , 3 months ago

How to get the user id from laravel passport token?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cortez.connelly , 3 months ago

@jerad 

To get the user id from a Laravel Passport token, you can use the auth()->user() method. Here is an example of how you can retrieve the user id from a token:

1
2
3
4
$user = auth()->user();
$user_id = $user->id;

echo $user_id;


This code snippet retrieves the authenticated user using the auth()->user() method and then accesses the user's id using the id property. You can use this method in your controllers, routes, or middleware to retrieve the user id from a token in your Laravel application.