【发布时间】:2021-03-23 14:16:53
【问题描述】:
您好,我正在尝试显示经过身份验证的用户,但出现此错误:
未定义变量:用户(查看: C:\wamp64\www\Blan\resources\views\users\index.blade.php)
这是我的代码:
用户控制器:
public function index()
{
return view('users.index')->with('user', Auth::user());
}
观点:
@extends('layouts.app')
@section('content')
<div class="card">
<div class="card-header">
Update Your Profile
</div>
<div class="card-body">
<table class="table table-hover">
<thead>
<th>Image</th>
<th>Name</th>
<th>Update</th>
</thead>
<tbody>
@if( $users -> count() > 0 )
@foreach ($users as $user)
<tr>
<td>
@if(isset($user->profile->avatar))
<img src="{{ asset($user->profile->avatar)}}" width="60px" height="60px" style="border-radius: 50%" alt="">
@else
No image
@endif
</td>
<td>
{{$user->name}}
</td>
<td>
<a href="#" class="btn btn-success">Update</a>
</td>
</tr>
@endforeach
@else
<tr>
<th colspan="5" class="text-center">No Users </th>
</tr>
@endif
</tbody>
</table>
</div>
</div>
@stop
但如果我在控制器中使用此代码返回所有用户:
return view('users.index')->with('users', User::all());
它工作正常。
那么我如何才能只返回当前经过身份验证的用户?
【问题讨论】:
-
这能回答你的问题吗? LARAVEL 5 :: Display 'username' on homepage?
标签: php mysql laravel laravel-6