FunCoding's avatar

auth()->id() global displaying error warning in VS Code

I have user model named Employee. I'm using this with Sanctum. This project is an api only, no frontend integration from Laravel. I'm using VS Code. The user global is triggering the ugly red line in VS Code. This typically how I use it:


$user_id = auth()->id();

I have a project where User is the model, and I get no warning. In this project Employee is the user model and I get the warning. In my auth config file, I have:


'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => env('AUTH_MODEL', App\Models\Employee\Employee::class),
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

I do have a working solution but it's ugly:


/** @var \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard $auth */

        $auth = auth();

        $user_id = $auth->id();

I've installed the laravel extension into vs code and also tried the ide helper that was recommended, I forget the name now, but it didn't work. I uninstalled the ide helper since it didn't work.

Anyone have a solution for this? Anyone experiencing the same? I really don't want to write the extra code and I won't allow the code to show the warning, even if it works. Any help is appreciated, thank you.

0 likes
6 replies
Snapey's avatar

check you don't have AUTH_MODEL in your env file as this will take priority

and that your Employee class extends authenticable

LaryAI's avatar
Level 58

The issue you're experiencing with VS Code is likely related to static analysis tools or extensions that are not recognizing the custom user model (Employee) as the authenticated user model. This can happen when the default User model is replaced with a custom model, and the tools are not configured to recognize this change.

Here are a few steps you can take to resolve this issue:

  1. Ensure Correct Configuration: Double-check your config/auth.php to ensure that the providers array is correctly set up to use your Employee model. It seems like you've already done this, but it's worth verifying.

  2. Use Laravel IDE Helper: The Laravel IDE Helper is a package that can help with static analysis by generating helper files for your IDE. If you haven't already, you can install it using Composer:

    composer require --dev barryvdh/laravel-ide-helper
    

    After installation, generate the helper files:

    php artisan ide-helper:generate
    

    This should help VS Code recognize the custom model.

  3. PHPDoc Annotations: You can add PHPDoc annotations to your code to help the IDE understand the types. For example, you can annotate the auth() call to specify the return type:

    /** @var \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard $auth */
    $auth = auth();
    
    $user_id = $auth->id();
    

    However, since you mentioned this is an "ugly" solution, you might want to try the next step.

  4. Custom User Model Type Hinting: If the above steps don't work, you can create a custom helper function or extend the auth() helper to return the correct type. This is a more advanced solution and might require some additional setup.

  5. Check Extensions: Ensure that your VS Code extensions are up to date. Sometimes, extensions like PHP Intelephense or Laravel Blade Snippets might need updates to recognize custom configurations.

  6. Clear Cache: Sometimes, clearing the cache can help:

    php artisan config:cache
    php artisan cache:clear
    

By following these steps, you should be able to resolve the warning in VS Code and have it recognize your custom Employee model as the authenticated user model.

silveira's avatar

do you found some fix for this? i am using vscode and te have same error

Please or to participate in this conversation.