Insert SQLite using an array - PHP / Laravel

1

I have the following array :

array:30 [▼
  0 => "12933"
  1 => "12931"
  2 => "12930"
  3 => "12929"
  4 => "12928"
  5 => "12927"
  6 => "12926"
  7 => "12925"
  8 => "12924"
  9 => "12923"
  10 => "12922"
  11 => "12921"
  12 => "12920"
  13 => "12919"
  14 => "12918"
  15 => "12917"
  16 => "12916"
  17 => "12915"
  18 => "12914"
  19 => "12913"
  20 => "12912"
  21 => "12911"
  22 => "12910"
  23 => "12909"
  24 => "12908"
  25 => "12907"
  26 => "12906"
  27 => "12905"
  28 => "12904"
  29 => "12903"
]

My application is using a production base SQLServer and I have another base SQLITE that I have only 2 query tables and on that basis, a table named Notas with only one column named Numero .

I am trying to give a insert to this table, in which every array would be a record, however, since I never did it, I am not getting it, I am trying the following code:

$insertNumeroNotas = DB::connection('sqlite')
     ->insert('insert into Notas (Numero) values (?)', $quantidadeNotas);

where $quantidadeNotas is array .

I need to first iterate through this array to then give insert , or insert need to be given in each iteration of that array?

    
asked by anonymous 15.11.2017 / 17:46

1 answer

1

Make a foreach and enclose the code of your question by changing the parameter so that each interaction records the item of each position of array :

foreach($quantidadeNotas as $n)
{
    DB::connection('sqlite')
        ->insert('insert into Notas (Numero) values (?)', [$n]);
}

this form would be one of the ways the other would be with:

DB::connection('sqlite')
      ->table('Notas')
      ->insert(array_map(function($i)
            { return ['Numero' => $i]; }, 
       $quantidadeNotas));

is another way where the algorithm itself solves the array .

References:

15.11.2017 / 21:15