Insert multiple rows of a single query using FOR

Asked

Viewed 74 times

0

I’m trying to input a lot of data using FOR but it’s not working. The impression is that he is not getting the data from FOR? inserir a descrição da imagem aqui

The expectation is that this happens:

ID|id_proposta|id_produto|quantidade|valor|total
1 |8          |1         |2         |250  |2000
2 |8          |2         |9         |300  |1000
3 |8          |3         |7         |150  |9000
4 |8          |4         |8         |200  |40000
5 |8          |5         |3         |290  |25000

Follows the code:

Controller

public function create()
    {


   $dadosForm = $this->request->all();

    $proposta = $this->proposta;



    $proposta->save([$dadosForm]); //salvo uma parte do for
    $proposta_id = $proposta->id; //pego o ID Salvo



     $count = $this->ensaios->max('id');
    $proposta_ensaios = $this->proposta_ensaios; //Essa parte fica em cinza parece que nao esta funcionando

    for($i=0;$i<=$count;$i++){

    $proposta_ensaios = new PropostaEnsaios();

    $proposta_ensaios->id_proposta = $proposta_id; //Aproveito o ID Salvo
    $proposta_ensaios->id_produto = $i;
    $proposta_ensaios->quantidade = $this->request->get('quantidade_'.$i);
    $proposta_ensaios->valor= $this->request->get('valor_'.$i);
    $proposta_ensaios->total = $this->request->get('total_'.$i);


    $proposta_ensaios->save([$dadosForm]);


    }
  • quantidade cannot come blank is what it says error. So has a problem in: $this->request->get('quantidade_'.$i);

  • Yes if you look a little further down it is not only the amount this coming in blank value and total tb in error are like "null" Without the for works but saves only 1 record and I have almost 30

  • You need to see if the values in the inputs can be given undefined index: ... Comments on the save() and make a echo '<pre>'; print_r($proposta_ensaios);

  • [quantity] => [value] => [total] =>

  • 1

    In place of $this->request->get('quantidade_'.$i) shouldn’t be then $dadosForm['chave'] ?

  • That’s right @rray! Comments your reply to me positive!

  • 1

    I think you better (if you want) create the answer I imagine you can give more details about how you solved the problem.

  • Hard when I comment I can only close the question after 2 days but that’s okay!

Show 3 more comments

1 answer

1


Problem solved with the help of @rray

Inside the FOR instead of using:

$proposta_ensaios->quantidade = $this->request->get('quantidade_'.$i);
$proposta_ensaios->valor= $this->request->get('valor_'.$i);
$proposta_ensaios->total = $this->request->get('total_'.$i);

I traded for:

    $proposta_ensaios->quantidade = $dadosForm['quantidade_'.$i];
    $proposta_ensaios->valor= $dadosForm['valor_'.$i];
    $proposta_ensaios->total = $dadosForm['total_'.$i];

Browser other questions tagged

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