0

我可以将角色分配给团队中的用户,并且可以获取附加到该用户的团队列表也可以获得相同用户的角色列表,但是当我在编辑刀片中汇总以获取附加到该用户的每个团队的角色时,结果重复...我的用户控制器:

 public function edit(User $user)
{  
    $teams = Team::all();
    $roles = Role::all();
        return view('admin.users.edit', compact('user','roles','teams'));
  
}

我的编辑刀片:

<table id="multi-select" class="display">
            <thead>
              <tr>
                <th>
                  <label>
                    <input type="checkbox" class="select-all" />
                    <span></span>
                  </label>
                </th>
                <th>Customer/Team</th>
                <th>Role</th>
                <th>Description</th>
              </tr>
            </thead>
            <tbody>
                 @foreach($user->rolesTeams as $team)
                      <tr>
                        <td>
                          <label>
                            <input type="checkbox" />
                            <span></span>
                          </label>
                        </td>
                        <td> {{ $team->display_name }}</td>
                       @foreach($user->roles as $role) 
                        <td> {{ $role->display_name }}</td>
                        <td> {{ $role->description }}</td>
                       @endforeach
                      </tr>
                @endforeach
            </tbody>
          </table>

从数据库 Sql编辑用户

预期:团队 ID 2 显示角色 ID1 团队 ID 3 显示角色 ID 2 任何帮助将不胜感激。

4

1 回答 1

0

我通过在 foreach 循环中使用 for 而不是 foreach 来解决这个问题,

 @for ($i=0; $i<count ($user->rolesTeams) ; $i++)
                  <tr>
                      <td>
                         <label>
                         <input type="checkbox" />
                         <span></span>
                         </label>
                      </td>
                      <td> {{ $user->rolesTeams[$i]->display_name }}</td>
                      <td> {{ $user->roles[$i]->display_name }}</td>
                      <td> {{ $user->roles[$i]->description }}</td>
                  </tr>
                @endfor
于 2020-10-10T11:11:06.927 回答