【问题标题】:Laravel - Laratrust how to list user with all the attached roles within a team?Laravel - Laratrust 如何列出团队中所有附加角色的用户?
【发布时间】:2020-10-10 10:26:23
【问题描述】:

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

 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>

Edit User 从数据库 Sql

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

【问题讨论】:

    标签: laravel laratrust


    【解决方案1】:

    我通过在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-24
      • 2019-08-31
      • 1970-01-01
      相关资源
      最近更新 更多