laravel查询,多余的行将被插入到表格发票详细信息中。例如FROM表包含6行,执行TO表后将有21行

2024-03-07

$ar = $po_id;
$variableAry=explode(",",$ar);
foreach($variableAry as $var1) {

    $details11=DB::table('po_estimations')
        ->where('po_number',$var1)
        ->select('*')
        ->get();

    foreach($details11 as $details)
    {
        $inserts[] = ['invoice_id' => $key,'shade' => $details_>project_shade,'unit' => $details->unit,'In_range' => $details->project_range,'brand_name' => $details->brand_name,'particulars_name' => $details->po_number,];
        DB::table('invoice_particulars')->insert($inserts); //saves redundant data
    }
}  

Like 米希尔·本德表明您不断向数组添加另一个元素,并在每次迭代时插入该数组。

目前正在发生以下事件:

loop details
    add an array to the end of $inserts
    $inserts contains 1 element
    insert $inserts
    1 element has been inserted in the database

    next iteration

    add an array to the end of $inserts
    $inserts contains 2 elements (1 from the previous iteration)
    insert $inserts
    2 elements have been inserted in the database

    next iteration

    add an array to the end of $inserts
    $inserts contains 3 elements (2 from the previous iterations)
    insert $inserts
    3 elements have been inserted in the database

    next iteration

    ...

正如你所看到的$inserts数组变得越来越大,因为之前添加的所有内容都保留了下来。

解决这个问题的一种方法是将DB::table(...)->insert($inserts)在循环之外。如果坚持只执行一个查询,您应该执行以下操作:

foreach($details11 as $details)
{
    $inserts[] = ['invoice_id' => $key,'shade' => $details_>project_shade,'unit' => $details->unit,'In_range' => $details->project_range,'brand_name' => $details->brand_name,'particulars_name' => $details->po_number,];
}

DB::table('invoice_particulars')->insert($inserts); //saves redundant data

否则米希尔·本德答案也能达到目的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

laravel查询,多余的行将被插入到表格发票详细信息中。例如FROM表包含6行,执行TO表后将有21行 的相关文章

随机推荐