Laravel Passport API: createToken get id

13,468

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;

Share:
13,468

Related videos on Youtube

Paul Hermans
Author by

Paul Hermans

Running a webhosting company. Writing some code. Walking in nature. Enjoying a good glass of beer and music.

Updated on June 04, 2022

Comments

  • Paul Hermans
    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
      Paul Hermans over 6 years
      The token is stored in the database, in the table oauth_access_tokens. There the token gets an ID.
  • Joshua Kissoon
    Joshua Kissoon over 6 years
    What is the name parameter for?
  • Paul Hermans
    Paul Hermans over 6 years
    It's just a descriptive name for the token you are creating.