laravel-8laravel-passportintelephense

Laravel 8: undefined method 'createToken' intelephense(1013)


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]);
}

Solution

  • 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.