How to show the selected users

2

I am trying to show the selected users by means of a multiple selection , but only the last selected value is returned to me.

This is my code where I send the selected data in the view:

<div class="col-xs-4">
         <div class="form-group">
            <select  name="operador[]" class="form-control select2" multiple="multiple" data-placeholder="Usuarios" style="width: 100%;">
                 @foreach($usuarios as $usuario)
                 <option value="{{$usuario->id}}">{{$usuario->name}}</option>
                  @endforeach
             </select>
              </div><!-- /.form-group -->
     </div>

And in the Controller I do the tour in this way:

$idoperador = $request->input("operador");
     $usuarios = [];
            foreach ($idoperador as $usuario){

              $usuarios= DB::table("users")
                            ->where("id","=",$usuario)
                            ->get();    
            }
            return view('verificar.listaCorte', compact(['usuarios']));

This is the view where I receive the data from the controller                       

                  <th>Operador</th>

                  <th></th>
                </tr>
                    @foreach($usuarios as $listado)
                <tr>
                  <td>{{$listado->name}}</td>
                </tr>
                 @endforeach
              </table>

Thank you in advance.

    
asked by A. Palacio 26.06.2017 в 10:10
source

2 answers

1

You can use the statement whereIn

$usuarios = DB::table("users")->whereIn("id", $request->operador)
            ->get();

return view('verificar.listaCorte', compact('usuarios'));
    
answered by 26.06.2017 / 20:42
source
1

In the controller when you make the query by the id of the user you are overwriting the variable $ users, hence it always shows you the last one selected. I would try something like this:

$idoperador = $request->input("operador");
     $usuarios = [];
            foreach ($idoperador as $usuario){

              array_push($usuarios,DB::table("users")
                            ->where("id","=",$usuario)
                            ->get());    
            }
            return view('verificar.listaCorte', compact(['usuarios']));

In this way we add the element (our case, a database record) to the end of the array $ users, without overwriting it.

    
answered by 26.06.2017 в 14:05