0
votes

After I create a new user I try to create token via

$user = User::where('user_id', '=', $userid)->get();
$user->createToken('name')->accessToken;

Then i got the following error:

Method Illuminate\Database\Eloquent\Collection::createToken does not exist.

Thanks

1

1 Answers

1
votes

You are calling for a collection of users when you use the get() method, which won't have the createToken method (which is exactly what the error message is telling you).

You need to call for a single User model:

$user = User::find($userid);

And then, assuming you have the createToken method on your User model, this should work.

EDIT per comments:

You may have some other issue that is preventing the creation of the token in addition to the original issue of the collection vs an object. Try to create the token first:

$user = User::find($userid);
$token = $user->createToken('name');

Then if you will either get an error (if your createToken method is incorrect, or the parameter 'name' is not correct, etc), or you will have the token, and can then draw the accessToken from the new variable, $token.

Like this:

$accessToken = $token->accessToken

Either way, this will give you the diagnostics to bug hunt.