我的数据库中有这两个表:
这是我的代码的一部分: 这是我存储新投诉的地方
public function storeComplaint($data)
{
$complaint = new Complaint;
$complaint->complainttype_id = $data->complainttype;
$complaint->restaurant_id = $data->restaurant;
$complaint->user_id = $data->vendor;
$complaint->date = $data->date;
$complaint->comments = $data->comments;
$complaint->save();
$this->storeComplaint_Product($complaint->id, $data->products); //THIS IS TO INSERT INTO THE COMPLAINT_PRODUCTS TABLE
return $complaint;
}
这是我存储在Complaint_Products表中的位置:
public function storeComplaint_Product($complaint_id, $products)
{
Complaint_Product::where('complaint_id', '=', $complaint_id)->delete();
if(!empty($products))
{
foreach($products as $product)
{
$complaint_product = new Complaint_Product;
$complaint_product->complaint_id = $complaint_id;
$complaint_product->product_id = $product;
$complaint_product->save();
}
}
}
如您所见,当我致电storeComplaint_Product时,我会发送投诉-> id和要存储的产品。当我刚刚创建该投诉时,仍然让我知道“无法添加或更新子行:外键约束失败”。它甚至向我显示了我要插入的信息,并且这些ID存在!!