Good afternoon people, I want to know how I can send more than one form request to my Controller to make the relationship in the bank?
I have a N: M relationship with User and Group, but when I submit it on the form it only considers one value in my form, but I need it to consider two or more values, basically I need the ids of the groups that the users is related in my controller, to be able to manipulate and make the relationships. I have researched and think I can do for ajax using JQuery, does anyone have any tips or do you know how I can accomplish this feat?
Thanks for your attention.
Form
<form action="{{ route('postGrupoUsuario') }}" method="post" class="centralizarNomes">
{{ csrf_field() }}
<div class="form-group">
<label class="Usuarios_Lista">Usuarios </label>
<select name="Usuario" class="form-control Selecionar_Usuario">
@foreach($listaUsuario as $usuario)
<option value="{{ $usuario->Usuario_ID }}"> {{ $usuario->Usuario_Nome }} </option>
@endforeach
</select>
</div>
<div class="table-responsive">
<table class="table table-hover table-striped table-bordered">
<thead>
<tr>
<th style="width: 65px;" class="text-center">ID</th>
<th>Nome do Grupo</th>
<th> Selecionar </th>
</tr>
</thead>
<tbody>
@foreach($listaGrupo as $grupo)
<tr>
<td class="text-center"> {{$grupo->Grupo_ID }}</td>
<td> <span class="font-medium"> {{$grupo->Grupo_Nome }}</span></td>
<td> <input type="checkbox" name="Grupo" id="grupo" value="{{ $grupo->Grupo_ID }}"> </input> </td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
<div class="btn_Adc_Cancelar">
<a class="btn btn-inverse waves-effect waves-light" href="{{ route('indexAdmin') }}" id="btn_cancelar">Cancelar</a>
<button type="submit" class="btn btn-success waves-effect waves-light m-r-10" id="btn_cadastrar">Cadastrar</button>
</div>
</form>
Controller
public function ModelRelacaoPost (Request $request) //Faz o relacionamento
{
$usuario_id = $request->get('Usuario'); //pega o id do usuário
$grupo_id = $request->get('Grupo');//pego id do grupo
$usuario = Usuario::find($usuario_id); //encontra com base na pesquisa
$grupo = Grupo::find($grupo_id);//encontra com base na pesquisa
$usuario->grupo()->attach($grupo); //faz o relacionamento
return redirect()->route('grupoUsuario'); //redireciona
}
Currently I can make the User's relationship with a Group more not for several groups. I'm using Laravel 5.5