Laravel 8: undefined method 'createToken' intelephense(1013)
Asked Answered
T

3

4

I have a problem with PHP intelephense, the method createToken is undefined. i don't know how to fix it. but when I run it in postman it works. i don't know why vscode doesnt recognize it. i also add the use Laravel\Passport\HasApiTokens; and use HasApiTokens in user model. please help me, I'm running out of options. thank you guys

public function login(Request $request)
{
    $login = $request->validate([
        'email' => 'required',
        'password'=> 'required',
    ]);
    if (!Auth::attempt($login)){
        return response()->json(['message' => 'error']);
    }
    $user = Auth::user();
    $token = $user->createToken('Token Name')->accessToken;
    return response()->json(['user' => $user, 'token' => $token]);
}
Thompson answered 5/10, 2021 at 2:57 Comment(0)
B
35

I encountered the same problem and solved by adding the line. For my case.

    /** @var \App\Models\MyUserModel $user **/
    $user = Auth::user();

I think the annotation line tell PHP intelephense that $user variable is not Illuminate\Foundation\Auth\User type but \App\Models\MyUserModel type. Please try it.

Blowfish answered 15/10, 2021 at 5:28 Comment(1)
A simple yet useful solution. Thank you.Boggart
C
1

in the model User add

use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
}

try and tell me

Canticle answered 8/10, 2021 at 19:49 Comment(3)
what's the problem? I'm trying to help you and you're giving me a minus 1? I assure you that this way you won't get far in programmingCanticle
tell me what the problem is and I will help youCanticle
@landrymoutongo I think you got a minus because the OP has mentioned in question that the HasApiTokens trait is already added in the User model. So your answer gives an impression that you wrote this answer without reading the question carefully.Sapele
M
0
  • Step 1:
    composer require --dev barryvdh/laravel-ide-helper
    
  • Step 2: Add the package to the extra.laravel.dont-discover key in composer.json; e.g.,
    "extra": {   "laravel": {
       "dont-discover": [
         "barryvdh/laravel-ide-helper"
       ]   } }
    
  • Step 3: Add the following class to the providers array in config/app.php:
    Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,
    
Mucronate answered 2/9 at 12:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.