Laravel Auth::user() has no code completion for User model functions

Viewed 2584

I am using Laravel and PHPStorm.

The auto completion works for all models, just not for the 'User', when I call it with Auth::user().

When I call the Auth::user() I get the right user object, but the return type of Auth::user() is Authenticatable and not User. So I get no code completion for my user object which is very anoying because I use it very often. I think the problem has something to do with the return type of Auth::user() because it is Authenticatable and not User.

In the config/auth.php I already set the model to app/User::class.

Can you tell me how to cast the return value to the User model?

In this example you can see the behaviour.

3 Answers

I know this is an old post, but I'm still seeing the issue, in spite of using ide-helper.

In my Laravel app, Eloquent model App\User is set for "User Providers" in /config/auth.php:

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

I just rebuilt the file _ide_helper.php by running: php artisan ide-helper:generate and this is what I see for Auth()::user()

     * Get the currently authenticated user.
     *
     * @return \App\User|null \\this is desired
     * @static 
     */ 
    public static function user()
    {
                    /** @var \Illuminate\Auth\SessionGuard $instance */
                    return $instance->user();
    }

Looking in the _ide_helper.php file, I see this:

enter image description here

Which results in undesired type hinting:

enter image description here

enter image description here

The end result, is that even with using the ide-helper, I'm still seeing the completion problems described by the OP.

The solution presented here solved a lot of related problems (the ide-helper package is a godsend), but I still have the OP's problem and the provided answer does not help.

Related