Laravel Passport API:createToken 获取 id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46771773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Passport API: createToken get id
提问by Paul Hermans
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
我正在使用 Laravel Passport API 通过个人访问令牌在 Laravel 和外部“代理”之间进行通信:https://laravel.com/docs/5.5/passport#personal-access-tokens
You can create tokens: via $token = \Auth::user()->createToken('name')->accessToken;
您可以创建令牌:通过 $token = \Auth::user()->createToken('name')->accessToken;
($token then holds only the token itself, not the object)
($token 然后只保存令牌本身,而不是对象)
Question
题
How can I get the token()->id
for a newly created token?
如何获取token()->id
新创建的令牌?
Background
背景
I need to get the ID to store it in the database to make relation to other table.
我需要获取 ID 以将其存储在数据库中以与其他表建立关系。
回答by Paul Hermans
You should split the token creation:
您应该拆分令牌创建:
First create the object, this returns a Laravel\Passport\PersonalAccessTokenResult Object:
首先创建对象,这将返回一个 Laravel\Passport\PersonalAccessTokenResult 对象:
$tokenobj = \Auth::user()->createToken('name');
$tokenobj = \Auth::user()->createToken('name');
Then you can get the accessToken itself via:
然后您可以通过以下方式获取 accessToken 本身:
$token = $tokenobj->accessToken;
$token = $tokenobj->accessToken;
And the token id via:
和令牌ID通过:
$token_id = $tokenobj->token->id;
$token_id = $tokenobj->token->id;