Невозможно добавить или обновить дочернюю строку, но внешний ключ существует - PullRequest
0 голосов
/ 10 июня 2019

В моей базе данных есть две таблицы:

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 и товары, которые я хочу сохранить. Тем не менее, я получаю сообщение «Невозможно добавить или обновить дочернюю строку: ограничение внешнего ключа не удается», когда я только что создал эту жалобу. Он даже показывает мне информацию, которую я пытаюсь вставить, и эти идентификаторы существуют !!

Lara

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...