我如何获得与用户登录具有相同项目的Username。使用Laravel和多对多关系。数据透视表user_projects具有user_id和project_id。
这是我的控制器
public function index()
{
$users = User::where('id', Auth::user()->id)->with('projects')->get();
return view ('teams.index', compact('users'));
}
此我的观看索引
<table class="table table-consended">
<tr>
<th> <h4>No</h4> </th>
<th> <h4>Name</h4> </th>
<th> <h4>Role</h4> </th>
<th> <h4>Project</h4> </th>
</tr>
@foreach($users as $item => $user)
<tr>
<td>{{$item+1}}</td>
<td>{{$user->name}}</td>
<td>{{$user->role->name}}</td>
<td>
@foreach ($user->projects as $project)
{{$project->project_name}}
@endforeach
</td>
</tr>
@endforeach()
</table>
但未添加项目时,视图为空白。