Seems duplicate of : how to get current user id in laravel 5.4
I am working on laravel 5.4, i have used the auth for user login at client side,now i want the logged in user details at the Controller,
view side by writing below code i got that:
{{ Auth::user()->name }} // this works on view page only.
Suggest me the library files with the code. I want to display some user data like name,age,dob,etc after logged in.
The laravel Auth
Facade is used to get the autheticated user data as
$user = Auth::user();
print_r($user);
This will work in your controller and view both, but you have to include it as
use Illuminate\Support\Facades\Auth;
Just use the helper function you won't need to instantiate or import any class.
$user = auth()->user();
then dd($user);
you'll have a full data on user.
you can then pull what you want.
$user->name
etc...
Laravel has helpler for that. u can use auth() anywhere. for example:
auth()->user()->name
or check if not authentificated:
if(! auth()->user()){}
You can use auth()->user->name
This should work
use Illuminate\Support\Facades\Auth;
// Get the currently authenticated user...
$user = Auth::user();
But you have to use use
You can access the user in any controller using
$user = Auth::user();
You should then be able to get details of the user by doing things like
$user_id = $user->id; //or Auth::user()->id;
$user_email = $user->email; // or Auth::user()->email;
See more details here https://laravel.com/docs/5.4/authentication#retrieving-the-authenticated-user
来源:https://stackoverflow.com/questions/45257981/laravel-5-4-how-to-get-logged-in-user-data-into-controller