dounayan3643 2013-11-15 19:06 采纳率: 0%
浏览 78
已采纳

使用laravel 4上的循环将数据数组存储到单个数组中

I was stuck on figuring how to store my arrays of data into a single one so I can insert it on the table using eloquent. I am using a javascript to add dynamic rows. Here's the 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();
};
});

and here's my html file

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

and in my controller when I tried to use

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

I got these result:

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)

I learned it from here: Generating New Array from Laravel 4 Input

my question is how can i put these arrays into a single array to come up like these code

$insert = array();

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

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

I also get the above code from here: [SOLVED] fluent query builder multiple insert with a foreach

  • 写回答

1条回答 默认 最新

  • dongtiannai0654 2013-11-15 19:15
    关注

    You insert multiple values by constructing an associative array wherein the keys are column names and the values are, well, the values. It isn't obvious why you are confused, since the example you provide is pretty much spot on:

    $inserts = array();
    foreach ( $input as $v ) {
        $inserts[] = array('name' => $v['name'], 'quantity' => $v['quantity']);
    }
    DB::table('your_table')->insert($inserts);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。