Taking data from the checkbox and sending it to the bank

Asked

Viewed 446 times

2

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

How would I send all the data from checkbox selected to the table conta_categoria?

My form create.Lade

<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 entering the data

if($bill->save()){

      $contasCategoria = new \App\ContasCategoria();

      $contasCategoria->id_conta = $ultimoID;

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

}

1 answer

2


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

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

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

See if this is the way 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);
}
  • Good.. I couldn’t do exactly how you still spoke with doubt.. I even saved it in the database when you selected only one.. but not with the array. I did not hit foreach to add arrays if($account->save()){&#xA;&#xA; $contasCategoria = new \Realito\ContasCategoria();&#xA;&#xA; $contasCategoria->id_conta = $ultimoID;&#xA;&#xA; $contasCategoria->id_categoria = $request->get('categoria');&#xA;&#xA; $conta->contasCategoria()->save($contasCategoria);&#xA;&#xTo; }

  • updates your question with the current code.

  • Ready... giving dd($request) it returns me the selected fields

  • na viu you will name the field 'Description[]' (with bracket). But in the controller you will use $request->get('category'); (no bracket).

  • Do the following, from a dd($request->get('category'));

  • I did that and he brought me the 4 selected categories, only remaining to send the 4 to the bank

  • Just you make a go or foreach now, saving one by one.

  • this foreach that I’m not doing right..

  • a glance, I added the code in my reply. See if this resolves.

  • thank you. had just resolved would close topic. But mt thanks for the help.. was of great use!

  • You’re welcome, buddy. A hug.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.