Laravel Passport API: createToken get id
You should split the token creation:
First create the object, this returns a Laravel\Passport\PersonalAccessTokenResult Object:
$tokenobj = \Auth::user()->createToken('name');
Then you can get the accessToken itself via:
$token = $tokenobj->accessToken;
And the token id via:
$token_id = $tokenobj->token->id;
Related videos on Youtube
Paul Hermans
Running a webhosting company. Writing some code. Walking in nature. Enjoying a good glass of beer and music.
Updated on June 04, 2022Comments
-
Paul Hermans almost 2 years
Situation
I'm using Laravel Passport API to communicate between Laravel and external "agents" via Personal Access Tokens: https://laravel.com/docs/5.5/passport#personal-access-tokens
You can create tokens: via
$token = \Auth::user()->createToken('name')->accessToken;
($token then holds only the token itself, not the object)
Question
How can I get the
token()->id
for a newly created token?Background
I need to get the ID to store it in the database to make relation to other table.
-
Paul Hermans over 6 yearsThe token is stored in the database, in the table
oauth_access_tokens
. There the token gets an ID.
-
-
Joshua Kissoon over 6 yearsWhat is the name parameter for?
-
Paul Hermans over 6 yearsIt's just a descriptive name for the token you are creating.