dprh34164 2015-11-30 11:39
浏览 28
已采纳

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

I have a function in controller where multiple row from a form is being saved it looks like this ,

  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'].'');

    }

So in the loop I am trying to save multiple row , But only the last row of the form is being saved i n my database . Any leads on what could be the issue .

Thanks

  • 写回答

1条回答 默认 最新

  • duanla1996 2015-11-30 12:06
    关注

    I got the answer fix is here

    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'].'');
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里