Armazenar arrays de dados em uma única matriz usando um loop no laravel 4
-
20-12-2019 - |
Pergunta
Eu estava preso em descobrir como armazenar o meu matrizes de dados em um único arquivo para que eu possa inseri-lo na tabela usando eloqüente.Eu estou usando um javascript para adicionar linhas dinâmicas.Aqui está a 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();
};
});
e aqui está o meu arquivo 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 no meu controlador de quando eu tentei usar o
$input = Input::get('product');
dd($input);
Eu tenho estes resultado:
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)
Aprendi isso aqui: Gerar uma Nova Matriz de Laravel 4 Entrada
a minha pergunta é como posso colocar essas matrizes em uma única matriz para vir até como estas código
$insert = array();
foreach($tab as $key => $value)
{
$insert[] = array(
'id_reservation' => $reservation_id,
'produit_id' => $key,
'quantite' => $value
);
}
DB::table('products')->insert($insert);
Eu também obter o código acima aqui: [RESOLVIDO] fluente construtor de consultas vários insert com um foreach
Solução
Você pode inserir vários valores, a construção de uma matriz associativa em que as chaves são nomes de coluna e os valores são, assim, os valores.Não é óbvio por que você está confuso, uma vez que o exemplo que você fornecer é muito bonito o lugar em:
$inserts = array();
foreach ( $input as $v ) {
$inserts[] = array('name' => $v['name'], 'quantity' => $v['quantity']);
}
DB::table('your_table')->insert($inserts);