Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 895
mmanuel Avatar asked Jul 05 '17 20:07

mmanuel


People also ask

What is auth ()- user () in Laravel?

Laravel includes built-in authentication and session services which are typically accessed via the Auth and Session facades. These features provide cookie-based authentication for requests that are initiated from web browsers. They provide methods that allow you to verify a user's credentials and authenticate the user.

What is the difference between auth () user () ID and Auth () ID?

Difference between auth()->user()->id and Auth::user()->id. Short explanation: No difference really, they both return an instance of Illuminate\Auth\AuthManager . So Auth:: and auth() in your examples will be the exact same object.

What does Auth :: check do in Laravel?

In other words, Auth::check() calls Auth::user() , gets the result from it, and then checks to see if the user exists. The main difference is that it checks if the user is null for you so that you get a boolean value. As you can see, it calls the user() method, checks if it's null, and then returns a boolean value.


2 Answers

Add laravel-ide-helper package to your project - https://github.com/barryvdh/laravel-ide-helper.

The package generates a help file for the IDE with all the Facades and their functions. It fixes the Facades auto-completion so Auth::user() is fixes too

like image 183
Idob Avatar answered Oct 06 '22 01:10

Idob


Auth::user()   

Auth is an Alias from ServiceProviders array in config/app.php if you directly use use Illuminate\Support\Facades\Auth; you will get a nice completion in PHPstorm as well as Sublime but it depends on the IDE.

like image 30
Romantic Dev Avatar answered Oct 05 '22 23:10

Romantic Dev