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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With