无法添加或更新子行,但存在外键

时间:2019-06-09 21:38:04

标签: mysql laravel laravel-5 eloquent

我的数据库中有这两个表:

enter image description here

enter image description here

这是我的代码的一部分: 这是我存储新投诉的地方

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存在!!

Lara

0 个答案:

没有答案
相关问题