Domanda

Sono rimasto bloccato a capire come conservare i miei array di dati in uno singolo in modo da poterlo inserire sulla tabella usando eloquente.Sto usando un JavaScript per aggiungere righe dinamiche.Ecco il JS:

$(function(){
var rowCount = document.getElementById('tblContacts').rows.length - 1 ;
var rowArrayId = rowCount ;

function addRow(){

    $("#tblContacts tbody").append(
        "<tr>"+
        "<td><input type='text' name='product[" + rowArrayId + "][name]' class='form-control'/></td>"+
        "<td><textarea name='product[" + rowArrayId + "][description]' class='form-control' rows='1'></textarea></td>"+
        "<td><input type='text' name='product[" + rowArrayId + "][quantity]' class='form-control'/></td>"+
        "<td><input type='text' name='product[" + rowArrayId + "][price]' class='form-control'/></td>"+
        "<td><button class='btnRemoveRow btn btn-danger'>Remove</button></td>"+
        "</tr>");

    $(".btnRemoveRow").bind("click", removeRow);

rowArrayId = rowArrayId + 1; };


function removeRow(){
    var par = $(this).parent().parent(); //tr
    par.remove();
};
});
.

Ed ecco il mio file HTML

<tr>
<td><input type='text' name='product[0][name]' class="form-control"/></td>
<td><textarea name='product[0][description]' class="form-control" rows="1"></textarea></td>
<td><input type='text' name='product[0][quantity]' class="form-control"/></td>
<td><input type='text' name='product[0][price]' class="form-control"/></td>
<td><button class="btnRemoveRow btn btn-danger">Remove</button></td>
 </tr>
$(".btnRemoveRow").bind("click", removeRow);
$("#btnAddRow").bind("click", addRow);          
.

E nel mio controller quando ho provato a usare

$input = Input::get('product');
dd($input);
.

Ho ottenuto questi risultati:

array (size=3)
0 => 
array (size=4)
  'name' => string 'first product' (length=13)
  'description' => string 'first product description' (length=25)
  'quantity' => string '10' (length=2)
  'price' => string '15' (length=2)
1 => 
array (size=4)
  'name' => string '2nd product ' (length=12)
  'description' => string '2nd product description' (length=23)
  'quantity' => string '20' (length=2)
  'price' => string '20' (length=2)
2 => 
array (size=4)
  'name' => string '3rd product ' (length=12)
  'description' => string '3rd product description' (length=23)
  'quantity' => string '25' (length=2)
  'price' => string '30' (length=2)
.

L'ho imparato da qui: Generazione di un nuovo array da Lariavel 4 Ingresso

La mia domanda è come posso mettere questi array in un singolo array per venire come questi codici

$insert = array();

foreach($tab as $key => $value)
{
$insert[] = array(
    'id_reservation' => $reservation_id,
    'produit_id' => $key,
    'quantite' => $value
);
}

DB::table('products')->insert($insert);
.

Io ottengo anche il codice sopra qui sopra: [Risolto] Fluent Query Builder Inserto multiplocon un foreach

È stato utile?

Soluzione

Si inseriscono più valori costruendo un array associativo in cui i tasti sono nomi di colonna e i valori sono, bene, i valori.Non è ovvio il motivo per cui sei confuso, dal momento che l'esempio che fornisci è praticamente un posto su:

$inserts = array();
foreach ( $input as $v ) {
    $inserts[] = array('name' => $v['name'], 'quantity' => $v['quantity']);
}
DB::table('your_table')->insert($inserts);
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top