创建具有特征HasApiTokens的用户失败

时间:2019-09-30 17:44:17

标签: laravel

我在Laravel 6中有这段代码。我认为这是正确的,但会出错

$input = $request->all();
$input['password'] = bcrypt($input['password']);
$user = User::create($input);
$success['token'] = $user->createToken('Albarid')->accessToken; // This line get error
$success['name'] = $user->name;

$domain = new Domain;
$domain->domain = $input['domain'];
$domain->user_id = $user->id;
$domain->save();

用户模型使用

use Laravel\Passport\HasApiTokens;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasApiTokens, Notifiable;

错误行大约在第98行的PersonalAccessTokenFactory

protected function createRequest($client, $userId, array $scopes)
{
    return (new ServerRequest)->withParsedBody([
        'grant_type' => 'personal_access',
        'client_id' => $client->id,  // Error is here
        'client_secret' => $client->secret,
        'user_id' => $userId,
        'scope' => implode(' ', $scopes),
    ]);
}

尝试捕获错误

{"success":false,"message":"Problem when saving on database","data":["Trying to get property 'id' of non-object",64]}

修补匠

>>> $success['token'] = $user->createToken('Albarid')->accessToken;
PHP Notice:  Trying to get property 'id' of non-object in /home/abkrim/Sites/albarid/vendor/laravel/passport/src/PersonalAccessTokenFactory.php on line 98

有什么想法吗?

Afetr以某种方式阅读了一些文章。

对我来说,没有合理的解释

重新运行

php artisan passport:install 
Personal access client created successfully.
Client ID: 3
Client secret: rWVS39yXsfvdAq9cbYR9MPQpiXNOsm9CYZrgxxH6
Password grant client created successfully.
Client ID: 4
Client secret: iRQZQ03uUjLXH2u2BHrY73tzef94UMcMK0yjO5B1

现在在auth_clients上有4个客户端可以正常工作

如果在此表中,我只有2行,则行不通。

为什么?

我花了很多时间解决这个问题。

0 个答案:

没有答案