我有两个表“发票”,其中“invoice_no”作为主键,“invoice_details”以“invoice_no”作为外键。我想使用 Laravel 同时将数据插入到两个表中。问题是当我提交表单时,数据被存储到发票表中,但没有存储到invoice_details中,这给了我外键为空的完整性约束违规错误。这是控制器代码:public function insertInv(Request $request){ //insert into invice $customer_name = $request->input('customer_name'); $c_email = $request->input('c_email'); $c_phone = $request->input('c_phone'); $billing_address = $request->input('billing_address'); $term = $request->input('term'); $order_date = $request->input('order_date'); $due_date = $request->input('due_date'); $discount = $request->input('discount'); $net_total = $request->input('net_total'); $paid = $request->input('paid'); $due = $request->input('due'); $payment_type = $request->input('payment_type'); $invoice_msg = $request->input('invoice_msg'); //insert into invoice_details $invoice_no = $request->input('invoice_no'); $product_name = $request->input('state'); $inven_name = $request->input('country'); $unit = $request->input('unit'); $price = $request->input('price'); $qty = $request->input('cqty'); $sub_total = $request->input('sub_total'); //insert into invoice $invo=array('customer_name'=>$customer_name,'c_email'=>$c_email,'c_phone'=>$c_phone,'billing_address'=>$billing_address,'term'=>$term,'order_date'=>$order_date,'due_date'=>$due_date,'discount'=>$discount,'net_total'=>$net_total,'paid'=>$paid,'due'=>$due,'payment_type'=>$payment_type,'invoice_msg'=>$invoice_msg ); DB::table('invoice')->insert($invo); //insert into invoice_details $invo_det=array('invoice_no'=>$invoice_no,'product_name'=>$product_name,'inven_name'=>$inven_name,'unit'=>$unit,'price'=>$price,'qty'=>$qty,'sub_total'=>$sub_total); DB::table('invoice_details')->insert($invo_det); return redirect()->back();}
1 回答
倚天杖
TA贡献1828条经验 获得超3个赞
您可以使用insertGetId方法:
$invoiceId= DB::table('invoice')->insertGetId($invo);
$invo_det=array('invoice_no'=>$invoiceId,'product_name'=>$product_name,'inven_name'=>$inven_name,'unit'=>$unit,'price'=>$price,'qty'=>$qty,'sub_total'=>$sub_total);
DB::table('invoice_details')->insert($invo_det);
- 1 回答
- 0 关注
- 104 浏览
添加回答
举报
0/150
提交
取消