How to save radio input inside an Laravel table

Asked

Viewed 208 times

0

inserir a descrição da imagem aqui

How to update the database in case id=54 updating radio inputs meet with value=a / purchase with value=c.

Obs:

This is a purchase request made by the user, I would like to give an update only in the attendpurchase field according to radio input above.

                   @foreach($detalhes as $detalhe)                        
                     <tr>                       

                      <td>                     
                          <div class="col-xs-6">
                          Atender <input type="radio" name="atendecompra[{{ print $i}}]" value="a" checked="true"> 
                          </div>
                           <div class="col-xs-6">
                          Comprar<input type="radio" name="atendecompra[{{ print $i}}]" value="c">
                          </div>
                      </td>                         
                     </tr>
                      <?php $i++;?>
                      @endforeach

Controllher

public function update(Request $request, $id){


       $idproduto=$request->get('idproduto');
      $atendecompra=$request->get('atendecompra'); 
       $cont = 0;
        while($cont < count($idproduto)){
          $detalhe = new RequisicaoDet();
          $detalhe->idrequisicao=$id;          
          $detalhe->atendecompra=String($atendecompra)[$cont];                  
          $detalhe->update();
          $cont=$cont+1;
        }
         return Redirect::to('almoxarifado/requisicao');
      }

Model

class RequisicaoDet extends Model
{
   protected $table         ='requisicaodet';
   protected $primaryKey    ='idrequisicaodet';
   public $timestamps       =false;
   protected$fillable       =[
   'idrequisicao',
   'idproduto',
   'qnt',
   'detalhe',
   'idcentrocusto',
   'atendecompra'
    ];
   protected $guarded       =[];
}
  • Welcome to Stackoverflow, the problem described in your question was not very clear, could edit to try to explain better

  • 1

    In your case is a flag, a number that identifies each radio, and with this you can compare and change the value when you want, missed even pass more information Model, Controller for us to have a bigger idea of the problem!

  • thanks for the feedback I will edit and pass the model and controller

1 answer

0

To update the Model instance in Laravel, you can use the following syntax:

RequisicaoDet::find($id)->update([
  'atendecompra' => $request->get('atendecompra')
]);

It wasn’t very clear why you did that while there with the idproduto, but with the above command will solve your question, which is how to update the atendecompra for the specific id.

If there is any doubt, say I will improve the answer.

  • I cannot do this because the name="atendecompra[{{ print $i}}] returns 0, it is inside a foreach, I use print $i to generate an index but when saving the offset error 0 or 1. Obs when I give dd($attendpurchase) brings right only at the time of saving that from this error.

  • gives a dd( $request->all() ) so we can see how it’s coming on the server

  • I don’t understand why you use the word print, try without print, like this: {{ $i }}

  • array:4 [ "_method" => "PATCH" "_token" => "mYd9B3TuMUAMQcJ3XZYa040nVXjr1luhrAi7tzGt" "idproduct" => array:2 [ 0 => "1" 1 => "1" ] "serve" => :2 [ "01" => "a" 11 => "a"&#Xa ];]

  • The request is coming right, and only at the time of the same update. The variable $i usso thus: <?php $i = 0;? > @foreach($details as $detail) <? php $i++;? > @endforeach

  • There’s no 0 position on the answering machine, and it looks like you start with $count = 0. E, if you want to convert the data into String, is closing the parenthesis in the wrong place, it should be like this: $detalhe->atendecompra=String($atendecompra[$cont]);

  • This giving this error - > Undefined offset: 0

  • Yes, this is happening because there are no elements in the array with this key.. Note that your array looks like this: [ "01" => "a" 11 => "a" ] , so to use a, will need to call offset [01]

  • If you use a crescent Count to iterate on atendecompra, You can take the {{ $i }} at render time: name="atendecompra[]"

  • Good morning David. Thank you for your return. It turns out that when shooting the variable i$ from attendpurchase[] the radio does not work within the view foreach,it loses the chorus line by line within the table, it would have another way?

Show 5 more comments

Browser other questions tagged

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