php laravel 5.4,如何将登录的用户数据获取到控制器中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45257981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:06:12  来源:igfitidea点击:

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

phplaravel-5.4

提问by Anand Maurya

Seems duplicate of : how to get current user id in laravel 5.4

似乎重复:如何在 laravel 5.4 中获取当前用户 ID

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,

我正在使用 laravel 5.4,我在客户端使用了用户登录身份验证,现在我想要在控制器上登录用户的详细信息,

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.

向我推荐带有代码的库文件。我想在登录后显示一些用户数据,如姓名、年龄、dob 等。

回答by RAUSHAN KUMAR

The laravel AuthFacade is used to get the autheticated user data as

laravel AuthFacade 用于获取经过身份验证的用户数据作为

$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;

回答by madeny

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.

那么dd($user);您将拥有关于用户的完整数据。

you can then pull what you want.

然后你可以拉你想要的东西。

$user->name

etc...

等等...

回答by WebArtisan

Laravel has helpler for that. u can use auth() anywhere. for example:

Laravel 对此有帮助。你可以在任何地方使用 auth()。例如:

auth()->user()->name

or check if not authentificated:

或检查是否未通过身份验证:

if(! auth()->user()){}

回答by Goundo

You can use auth()->user->name

您可以使用 auth()->user->name

回答by Vuk Stankovi?

This should work

这应该工作

use Illuminate\Support\Facades\Auth;

// Get the currently authenticated user...
$user = Auth::user();

But you have to use use

但是你必须使用 use

回答by The Sammie

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://laravel.com/docs/5.4/authentication#retrieving-the-authenticated-user

回答by Ravi

Use the facade

使用门面

use Illuminate\Support\Facades\Auth;

And

$user = Auth::user();

NOTE: You can call specific column in this library like $user = Auth::user()->name;or ->addressetc.

注意:您可以调用此库中的特定列,例如$user = Auth::user()->name;->address等。