Laravel-导入Excel保持循环

时间:2019-05-28 23:38:24

标签: php excel laravel

亲爱的

我有一个包含5K行的excel文件,并且已成功将其导入数据库中的表中。

但是错误,当系统完成所有行时,它会继续循环运行,并且页面不会停止运行,也不会重定向到我的视图。

我的控制器:

if($request->hasFile('import_file')){
       $path = $request->file('import_file')->getRealPath();
       $data = \Excel::load($path)->get();
       foreach ($data as $key => $row) {
       $res = policies::where('phone', '=', $row['phone'])
                      ->where('draft_no', '=', $row['draftno'])
                      ->where('due_date', '=', $duedate)
                      ->select('id')->get()->toArray();
                  if(empty($res)) {
                      $polic = new policies();
                      $polic->cust_id = $row['custno'];
                      $polic->policy = '';
                      $polic->bord_date = $borddate;
                      $polic->client_id = $row['clientid'];
                      $polic->client_no = $row['clientno'];
                      $polic->client_name = $row['clientname'];
                      $polic->draft_no = $row['draftno'];
                      if ($row['status'] == '') {
                          $polic->status = '';
                      } else {
                          $polic->status = $row['status'];
                      }

                      $polic->due_date = $duedate;
                      if ($row['curno'] == 'USD') {
                          $polic->currency = 1;
                      } else {
                          $polic->currency = 0;
                      }
                      $polic->amount = $row['amnt'];
                      $polic->zone = $row['zone'];
                      $polic->broker_id = $row['brokercode'];
                      $polic->broker_name = $row['brokername'];
                      $polic->remarks = $row['remarks'];
                      $polic->phone = $row['phone'];
                      $polic->insured_name = $row['insname'];
                      // $polic->cust_id = $row['valuedate'];
                      $polic->address = ''; //address
                      if (trim($row['status']) == 'P') {
                          $polic->paid_at = date('Y-m-d');
                      }
                      $polic->new = 1; //address
                      $polic->save();
       }
       else { 
       //am updating the imported date in the DB
       }

奇怪的是,我的本地主机工作正常,但是在digitaloceans云中,保持循环而不重定向。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我可能是因为您要插入5000行,而5000插入操作会消耗大量内存。您可以尝试的是批量插入操作。

在您的policy.php中,使所有字段均可填写

受保护的$ fillable = ['cust_id','policy','bord_date','client_id','client_no','client_name','draft_no','bord_date','status','due_date','currency ','amount','zone','broker_id','broker_name','remarks','phone','insured_name','address','paid_at','new'];

在您的excel文件中,使用导入而不是获取集合。

if($request->hasFile('import_file')){
       $path = $request->file('import_file')->getRealPath();
       $data = \Excel::load($path)->get();
$data=[];    
$i=0;  
 foreach ($data as $key => $row) {
       $res = policies::where('phone', '=', $row['phone'])
                      ->where('draft_no', '=', $row['draftno'])
                      ->where('due_date', '=', $duedate)
                      ->exists();
                  if(!$res) {
                    $i++;
                    $data[$i]['cust_id ']=$row['custno'];

                      $data['policy'] = '';
                      $data['bord_date'] = $borddate;
                      $data[$i]['client_id'] = $row['clientid'];
                      $data[$i]['client_no'] = $row['clientno'];
                      $data[$i]['client_name'] = $row['clientname'];
                      $data[$i]['draft_no'] = $row['draftno'];
                      if ($row['status'] == '') {
                          $data[$i]['status'] = '';
                      } else {
                          $data[$i]['status'] = $row['status'];
                      }

                      $data[$i]['due_date'] = $duedate;
                      if ($row['curno'] == 'USD') {
                          $data[$i]['currency'] = 1;
                      } else {
                          $data[$i]['currency'] = 0;
                      }
                      $data[$i]['amount'] = $row['amnt'];
                      $data[$i]['zone'] = $row['zone'];
                      $data[$i]['broker_id'] = $row['brokercode'];
                      $data[$i]['broker_name'] = $row['brokername'];
                      $data[$i]['remarks'] = $row['remarks'];
                      $data[$i]['phone'] = $row['phone'];
                      $data[$i]['insured_name'] = $row['insname'];
                      // $data[$i]['cust_id'] = $row['valuedate'];
                      $data[$i]['address'] = ''; //address
                      if (trim($row['status']) == 'P') {
                          $data[$i]['paid_at'] = date('Y-m-d');
                      }
                      $data[$i]['new'] = 1; //address

       }
       else { 
       //am updating the imported date in the DB
       }
}
policies::insert($data);