Laravel 5.5数据两次插入数据库

时间:2018-06-27 12:27:44

标签: laravel

此代码在订单表中输入了多个产品,但是发生的一件奇怪的事情是,如果我插入2个产品,则要输入4次数据,如果我插入1个产品,则要进行2次惰化。此代码有什么问题?

foreach($request->product_id as $rpk => $rpv)

    {
        $data = new Order();
        $data->user_id = $request->customer_id[$rpk];
        $data->status = $request->status[$rpk];
        $data->save();

        Order::where('id', $data->id)->update(['order_number' => $data->id]);

        // For OrderDetail
        $detail = new OrderDetail();
        $detail->customer_id = $request->customer_id[$rpk];
        $detail->order_id = $data->id;
        $detail->product_id = $request->product_id[$rpk];
        $detail->price = $request->price[$rpk];
        $detail->quantity = $request->quantity[$rpk];
        $detail->total = $request->total[$rpk];
        $detail->comment = $request->comment[0];
        $detail->date = date('Y-m-d H:i:s');
        $detail->bill_date = date('Y-m-d H:i:s');
        //$detail->sales_tax = '13'; // Need to change this
        $success = $detail->save();


    }

0 个答案:

没有答案