Picking up data from the checkbox and sending it to the bank

2

I'm creating a form and I need to send the selected checkboxes to the bank. I have the table conta , conta_categoria and categoria . In conta_categoria I have id_conta and id_categoria .

How would I send all selected checkbox data to conta_categoria ?

My form create.blade

<div class="form-group">
    {!! Form::label('descricao', 'Categoria: ') !!}
    @foreach (Realito\Categoria::all() as $categoria)
        <br>
        {!! Form::checkbox('descricao[]',$categoria->id_categoria) !!}
        {{ $categoria->id    }}
        {{ $categoria->descricao }}
        </br>
    @endforeach
</div>

My account controller where you are inserting the data

if ($ account-> save ()) {

      $contasCategoria = new \App\ContasCategoria();

      $contasCategoria->id_conta = $ultimoID;

      $contasCategoria->id_categoria = $request->get('categoria[]');
        // $contasCategoria->id_categoria = $codCategoria;
      $conta->contasCategoria()->save($contasCategoria);

}
    
asked by anonymous 11.06.2015 / 22:41

1 answer

2

If you change the name of the checkbox to description [], this way:

{!! Form::checkbox('descricao[]',$categoria->id_categoria) !!}

In your controller, you will receive an array with only the selected categories. Type thus $request->get('descricao') = Array([0] => 100, [1] => 103, [2] => 105); In this case you can make a foreach or else create a method to save several categories at once.

See if you decide to save the categories.

$categorias = $request->get('categoria[]');
foreach($categorias as $categoria){
      $contasCategoria = new \App\ContasCategoria();

      $contasCategoria->id_conta = $ultimoID;

      $contasCategoria->id_categoria = $categoria;
      $conta->contasCategoria()->save($contasCategoria);
}
    
11.06.2015 / 22:58