How to insert multiple rows from a single query using the same ID

1

I'm trying to make multiple inserts using the same ID with Laravel but it's only doing 1 insert.

The idea would look something like this.

ped_cod|est_cod|ped_qtde
    8  |2      |9
    8  |3      |2
    8  |4      |2
    8  |9      |15

MODEL

class EstoqueDet extends Model
{
    protected $table = 'public.estoque_det';
    protected $primaryKey = 'ped_cod';
}

CONTROLLER (Check if input is bringing something if Yes does insert)

$caneta  = $this->request->get('caneta_input');
$sulfite = $this->request->get('sulfite_input');
$tonner = $this->request->get('tonner_input');
$pasta = $this->request->get('pasta_input');



$estoqdet = $this->estoqueDet; 


if(isset($caneta)) {

    $estoqdet->ped_cod = $pedido_id;
    $estoqdet->est_cod = $cod_caneta;
    $estoqdet->ped_qtde = $caneta;
    $estoqdet->save([$estoqdet]);



}

if(isset($sulfite)) {

    $estoqdet->ped_cod = $pedido_id;
    $estoqdet->est_cod = $cod_sulfite;
    $estoqdet->ped_qtde = $sulfite;
    $estoqdet->save([$estoqdet]);


}

if(isset($tonner)) {

    $estoqdet->ped_cod = $pedido_id;
    $estoqdet->est_cod = $cod_tonner;
    $estoqdet->ped_qtde = $tonner;
    $estoqdet->save([$estoqdet]);


}

if(isset($pasta)) {

    $estoqdet->ped_cod = $pedido_id;
    $estoqdet->est_cod = $cod_pasta;
    $estoqdet->ped_qtde = $pasta;
    $estoqdet->save([$estoqdet]);


}
    
asked by anonymous 22.09.2016 / 16:43

1 answer

1

You need to create a new instance with each new record you want to create, in your case you are only changing the same record N times.

if(isset($caneta)) {
    $estoqdet = new EstoqueDet();
    $estoqdet->ped_cod = $pedido_id;
    $estoqdet->est_cod = $cod_caneta;
    $estoqdet->ped_qtde = $caneta;
    $estoqdet->save([$estoqdet]);
}

if(isset($sulfite)) {
    $estoqdet = new EstoqueDet();
    $estoqdet->ped_cod = $pedido_id;
    $estoqdet->est_cod = $cod_sulfite;
    $estoqdet->ped_qtde = $sulfite;
    $estoqdet->save([$estoqdet]);
}
    
22.09.2016 / 17:39