Save checkbox inside While Laravel

Asked

Viewed 95 times

0

I’d like to know how to save values checkbox within a while, the field comprar01 the checkbox who must return true or false.

I receive through the request():

$idproduto=$request->get('idproduto');
$qnt=$request->get('qnt'); 
$comprar01=$request->get('comprar01');

I do the while in the controller to save in the bank

$cont = 0;
while($cont < count($idproduto))
{
    $detalhe = new MapaCompraDet();
    $detalhe->idmapacompra = $dados->idmapacompra;
    $detalhe->idproduto = $idproduto[$cont];
    $detalhe->qnt = $qnt[$cont];     
    $detalhe->comprar01=$comprar01[$cont]; //aqui o problema
    $detalhe->save();
    $cont = $cont + 1;

Error:

inserir a descrição da imagem aqui

I tried that

public function update(Request $request, $id){
  $dados = MapaCompra::findOrFail($id);        
  $idproduto=$request->get('idproduto');
  for ($i=0; $i < count($idproduto) ; $i++) {
      $comprar01 = (isset($request['comprar01']))?$request['comprar01']:'inativo';
      dd($comprar01);
  }

He brings me in dd only those marked, when not mark it does not bring. I searched here and saw that I have to put indices in the checkbox before entering, I am trying. Behold: https://cursos.alura.com.br/forum/topico-inserindo-valores-de-checkbox-no-banco-de-dados-44489

MY FORM HAS AN ARRAY inserir a descrição da imagem aqui

CODE

  <tbody id="tbodyCotacao">
                    @foreach($detalhes as $detalhe)
                      <tr>
                        <!-- ITENS COTAÇÃO 01 -->
                       <td>
                         <input style=" width: 60px" class="form-control" type="number" min="0" name="entrega01[]" value="{{$detalhe->entrega01}}">
                       </td>
                       <td>
                          <input style=" width: 70px" class="form-control" type="text" name="marca01[]" value="{{$detalhe->marca01}}">
                       </td>
                       <td>
                         <input id="pqnt" type="hidden" name="qnt[]" value="{{$detalhe->qnt}}">
                         <input id="vrunit01" style=" width: 65px;text-align:center" class="form-control" type="text" name="vrunit01[]" value="{{number_format($detalhe->vrunit01, 2, ',', '.') }}" onblur="multiplicar();" onblur="formatar();">
                       </td>
                       <td>
                         <input id="subtotal01" style="text-align:right;width: 75px" class="form-control subtotal01" type="hidden" name="subtotal01[]" readonly onblur="calcular();">
                         <input  style="text-align:right;width: 75px" type="text" class="form-control subtotal01T" id="subtotal01T" value="{{number_format($detalhe->qnt*$detalhe->vrunit01, 2, ',', '.') }}" readonly>
                       <td style="text-align:center">
                           <input @if($detalhe->comprar01=='c') {!! 'checked="checked" ' !!}@endif type="checkbox"  name="comprar01[]" value="c">
                       </td></tr>
  • It’s complicated to do that with checkbox, because PHP at the time of bringing information only brings what was selected, if it exists in the middle checkbox without selecting your array $comprar01 is smaller than the others and will definitely give the question error. I would use one <select>.

  • 1

    True, he brought only the checked when not checked for error.

  • How would it be with select? Give me an example please...

  • In each line a combo with two options and value 0 and 1 with label no and yes

  • 1

    Entrendi, so it would be easy even, but my field this small to by a select, would have to be a checkbox even. Funny that in my registration worked normal, on this screen to edit that is giving this error.

  • 3
  • Take a look at this answer: https://answall.com/a/5716/54880 this is your solution, I even indicated your question as duplicate

  • Good morning Virgilio, I had already seen these topics, however I am testing but it is not working, it only takes the first items of the list. EX: If I have 3 lines, mark the 3 he picks the 1 understood. I’m trying to adjust but I still could not.

  • You did what??? Post your html

Show 4 more comments

1 answer

0

Do so, apply a default value:

$comprar01 = $request->get('comprar01', 0);

OR

$comprar01 = $request->has('comprar01') ? 1 : 0;

In fields like 'active', which are Boolean in Migrations, and in the TINYINT database, before saving/updating I do the following.

$request->merge(['active' => $request->has('active')]);
$model->update($request->all());

At you;

  • All right, Cau, can you fill me in on that active part, please? First I get the request $comprar01 = $request->get('comprar01', 0); and to put in a loop to save?

  • You have to see what comes in your field. By your code I assume it is an integer ($Count)... so you can do it like this. $comprar01 = $request->get('comprar01', 0); ... That is, if the checkbox is checked, it will take its value, if it has not checked it will take 0 as default. You set the default value according to your code... as you wish. Or you can use merge before saving/updating your data as I explained above. If you don’t check the checkbox value a request comes null. Use the request’s has(), get('key', $default) and merge([]) methods to work your code.

  • The example I gave in the answer I assumed that your field was true/false... so your case can do so too: $comprar01 = $request->has('comprar01') ? $request->get('comprar01') : 0;

  • Now if your checkbox can be tmb array. It is better to post the html form.

  • Yes my checkboxs are array are inside a dynamic table, I will graze....

  • Bro, your problem is not that simple. You have to dynamically set the checkbox name attribute according to the detail. Otherwise you won’t be able to connect one field to the other. In the details foreach do something like. name="buy-{{ $detail->id }}" ... Or something like that. Then you can apply the techniques I mentioned.

  • And I know, I thought it would be easier. I will follow the advice of our friend Cecilio to put a combobox even, I tested and here and was quiet, it is a shame it would be cool with ckeckbox. Thank you.

  • If you think it’s cool, do it with checkbox guy. Try and debug the code... It’s simple... dd($request->all()) in the controller and you see exactly what’s coming in the request, so you’ll know what needs to be done.

  • I debugged, it only brings the checkbox checked, if you have not checked it does not bring anything

  • This is how it should be. Only the marked ones come... But you have the Names of those who could be marked.... In foreach you see... $model->attribute = $request->get('checkbox' . $detail->id, $valor_unchecked); or $model->attribute = $request->has('checkbox' . $detail->id') ? $request->get('checkbox' . $detail->id') : $default; Easy dude. Just fetch the Names that should be...

  • I’ll try here and return

Show 6 more comments

Browser other questions tagged

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