laravel 5.4,How to get logged in user data into controller

て烟熏妆下的殇ゞ 提交于 2019-12-01 22:51:47

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!