Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Passport API: createToken get id

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.

like image 435
Paul Hermans Avatar asked Dec 24 '22 13:12

Paul Hermans


1 Answers

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;

like image 196
Paul Hermans Avatar answered Dec 28 '22 05:12

Paul Hermans