Храните массивы данных в один массив, используя цикл на Laravel 4

StackOverflow https://stackoverflow.com//questions/20008687

Вопрос

Я застрял на фигуру, как хранить свои массивы данных в один, чтобы я мог вставить его на таблицу, используя красноречие.Я использую JavaScript, чтобы добавить динамические ряды.Вот 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();
};
});
.

и вот мой 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);          
.

и в моем контроллере, когда я пытался использовать

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

Я получил этот результат:

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)
.

Я узнал это отсюда: Генерация нового массива от Laravel 4 Вход

Мой вопрос в том, как я могу вставить эти массивы в один массив, чтобы придумать, как этот код

$insert = array();

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

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

Я также получаю вышеуказанный код отсюда: [Решено] Свободный запросС foreach

Это было полезно?

Решение

Вы вставляете несколько значений, построив ассоциативный массив, в котором клавиши являются именами столбцов, а значения, ну, ну, значения.Не очевидно, почему вы запутались, поскольку пример, который вы предоставляете, довольно много места:

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top