如何在laravel 5 Controller中保存表单中的多行

时间:2015-11-30 11:39:53

标签: php laravel-5.2

我在控制器中有一个函数,其中保存了表单中的多行,它看起来像这样,

  public function postPayment(PaymentRequest $request){

                $insertPayment=Input::get();

                $payment= new paymentrecieved();

                $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];

                $payment->invoice_id=$insertPayment['invoiceid'];
                $payment->recieved_amount = $insertPayment['recieved_amount'][$i];
                $payment->refno = $insertPayment['ref_no'][$i];
                $payment->date = $insertPayment['date'][$i];
                $payment->adjust_amount = $insertPayment['adjustmentamount'][$i];
                $payment->adjust_mode= $adjust_mode;
                $payment->save();


         }

         // $result = paymentrecieved::create($data);
          $this->saveComment($insertPayment['comment'],$insertPayment['date1'],$insertPayment['invoiceid']);
          $request->session()->flash('alert-success', 'Payment Has Been inserted Successfully');
          return redirect('collection/payment/'.$insertPayment['invoiceid'].'');

    }

所以在循环中我试图保存多行,但只有表格的最后一行被保存在我的数据库中。可能是问题的任何线索。

谢谢

1 个答案:

答案 0 :(得分:0)

我得到的答案是在这里

public function postPayment(PaymentRequest $request){

                $insertPayment=Input::get();

                $data=array();

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

                $payment= new paymentrecieved();
                if($insertPayment['adjustmentmode'][$i]=='Option')
                $adjust_mode='NONE';
                else
                  $adjust_mode=$insertPayment['adjustmentmode'][$i];

                $payment->invoice_id=$insertPayment['invoiceid'];
                $payment->recieved_amount = $insertPayment['recieved_amount'][$i];
                $payment->refno = $insertPayment['ref_no'][$i];
                $payment->date = $insertPayment['date'][$i];
                $payment->adjust_amount = $insertPayment['adjustmentamount'][$i];
                $payment->adjust_mode= $adjust_mode;
                $payment->save();


         }

         // $result = paymentrecieved::create($data);
          $this->saveComment($insertPayment['comment'],$insertPayment['date1'],$insertPayment['invoiceid']);
          $request->session()->flash('alert-success', 'Payment Has Been inserted Successfully');
          return redirect('collection/payment/'.$insertPayment['invoiceid'].'');

    }