I have these two tables on my database:
And this is part of my code: Here is where I store the new complaint
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;
}
This is where I store into the Complaint_Products table:
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();
}
}
}
As you can see, when I call storeComplaint_Product I send the complaint->id and the products I want to store. still it gets me that "Cannot add or update a child row: a foreign key constraint fails" when I just created that complaint. It even shows me the information I'm trying to insert and those ids exists!!
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire