如何通过查询生成器在Laravel中插入具有倍数值的倍数数组

时间:2018-10-11 10:42:07

标签: insert

让我们假设我要插入一个数组,该容器跟随数据;

这是一个数组,

Array
(
    [0] => 1
    [1] => ::1
    [2] => Array
        (
            [0] => 1
        )

    [3] => Array
        (
            [0] => 0
            [1] => 1
        )

    [4] => 2018-10-11
    [5] => 2018-10-31
    [6] => 0
    [7] => 0
    [8] => 2018-10-11
    [9] => 10:38:36
)

但是我陷入了如何使用单个foreach循环插入此数组的问题

我正在使用此代码,但不适用于具有不同值的多个数组

$count_row = 0;
foreach($products as $row){
    $count_row ++;
    //Set Field data according to table column
    $data = array(
        'user_id' => $user_id,
        'ip_address' => $ip_address,
        'product_id' => $row,
        'page_id' => $row[$count_row],
        'start_date' => $start_date,
        'end_date' => $end_date,
        'type' => $type,
        'status' => $status,
        'created_date' => $created_date,
        'created_time' => $created_time,
    );

    //Query For Inserting Data
    $query = DB::table('tbl_product_advertisements')
                 ->insertGetId($data);
}

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,要插入这种类型的数组,我们必须使用2个循环,