Laravel - Laratrust how to list user with all the attached roles within a team?

1.2k views Asked by At

I can assign Role to User within a team and can get team list attached to that user also can get list of Roles to same user, but when I put together to get the Role of each Team attached to that user in the Edit blade, the results get duplicated... My UserController:

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

my edit blade:

<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 from Data-base Sql

expected : Team Id 2 to display Role Id1 Team Id 3 to display Role Id 2 any help will be appreciated..

1

There are 1 answers

0
luay adnan On

I figured it out by using for instead of foreach inside foreach loop,

 @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