Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5-如何在控制器中获取身份验证用户ID,检索用户数据并在视图中显示该数据_Php_Laravel 5 - Fatal编程技术网

Php Laravel 5-如何在控制器中获取身份验证用户ID,检索用户数据并在视图中显示该数据

Php Laravel 5-如何在控制器中获取身份验证用户ID,检索用户数据并在视图中显示该数据,php,laravel-5,Php,Laravel 5,对于Laravel5框架(和OOP),我想创建一个视图,用户可以在登录时查看/编辑自己的用户配置文件 在控制器中获取身份验证用户ID、检索用户数据(从DB)并在纵断面图中显示该数据的语法是什么 我尝试了以下方法,但不起作用 控制器: /** * Show the application user profile to the user. * * @return Response */ public function profile() { $users = User::find

对于Laravel5框架(和OOP),我想创建一个视图,用户可以在登录时查看/编辑自己的用户配置文件

在控制器中获取身份验证用户ID、检索用户数据(从DB)并在纵断面图中显示该数据的语法是什么

我尝试了以下方法,但不起作用

控制器:

/**
 * Show the application user profile to the user.
 *
 * @return Response
 */
public function profile()
{

    $users = User::find(Auth::user()->id);

    return view('profile')->with(['users' => $users]);
}
纵断面图:

<div class="row">
    <div class="col-md-10 col-md-offset-1">
        <div class="panel panel-default">
            <div class="panel-heading">Profil</div>

            <div class="panel-body">
                Your user id is: {{ Auth::user()->id }}.

    <table>
@foreach($users as $user)
    <tr>
        <td>{{ $user->first_name }}</td>
        <td>{{ $user->last_name }}</td>

    </tr>
@endforeach
    </table>



            </div>
        </div>
    </div>
</div>

Profil
您的用户id是:{{Auth::user()->id}。
@foreach($users作为$user)
{{$user->first_name}
{{$user->last_name}
@endforeach

谢谢。如果您尝试显示用户,但只有一个用户(已登录的用户),我们将非常感谢您提供的任何帮助。您还可以通过使用Auth facade而不进行其他数据库调用来获取相同的用户,从而简化它

控制器:

public function profile()
{
    $user = Auth::user();
    return view('profile')->with(['user' => $user]);
}
视图:


您的用户id是:{{$user->id}
您的名字是:{{$user->first_name}
您的姓是:{{$user->last_name}

find()
返回一个模型,而不是模型的集合,因此不要使用
foreach
,只需访问我添加的属性使用Auth;在我的控制器的顶部,它工作了!!你太棒了!非常感谢你的授权;这是强制性的,所以搜索者不会再混淆一个错误来纠正自己。
<div class="panel-body">
    Your user id is: {{ $user->id }}
    Your first name is: {{ $user->first_name }}
    Your last name is: {{ $user->last_name }}
</div>