mardi 10 novembre 2015

Laravel: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails in Laravel5

Guys I am trying to save in database like this

  public function postPayment(PaymentRequest $request){

          $insertPayment=Input::get();

          $payment= new paymentrecieved();
          $comment= new comments();

         $data=array();

          for($i = 0; $i < count($insertPayment['recieved_amount']); $i++) {

                if($insertPayment['adjustmentmode'][$i]=='Option')
                $adjust_mode='NONE';
                else
                  $adjust_mode=$insertPayment['adjustmentmode'][$i];
                  $data[] = [
                       'invoice_id' => $insertPayment['invoiceid'],
                       'recieved_amount' => $insertPayment['recieved_amount'][$i],
                      'refno' => $insertPayment['ref_no'][$i],
                      'date' => $insertPayment['date'][$i],
                       'adjust_mode' => $adjust_mode,
                       'adjust_amount' => $insertPayment['adjustmentamount'][$i]
                   ];

         }

          $result = paymentrecieved::create($data);

}

But I am getting the above error , I understood this is happening because I have Foreign Key in my model .

But I am not able to fix this

Can any one help me

Thanks & Regards



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire