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 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分