获取与登录用户具有相同项目的用户名

时间:2018-10-24 05:44:21

标签: laravel eloquent many-to-many

我如何获得与用户登录具有相同项目的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>

data table

view index

my final view index

data

但未添加项目时,视图为空白。

0 个答案:

没有答案
相关问题