2016-06-03 3 views
0

나는 invoice, invoiceduedates, payments.So라는 3 개의 테이블을 가진 능변적인 쿼리를 사용하고 있는데, 나는 export하는 모든 데이터를 laravel에서 아래의 쿼리를 사용하여 Excel로 엑셀한다.Excel 다운로드 - Laravel 5.1

| Due Date1 | Due Amount1 | Due Date2    | Due Amount2 | AdjustMode1   | Rcv Amount1 | AdjustMode2   | Rcv Amount2 | 
|------------|-------------|-----------------------|-------------|-----------------------|-------------|-----------------------|-------------| 
| 2016-03-25 | 2000  | 2016-02-29   | 2000  | Overseas Bank Charges | 2000  | Overseas Bank Charges | 2743  | 
| 2016-03-31 | 3750  | Overseas Bank Charges | 3708  | Overseas Bank Charges | 2750  |      |    | 

그러나 때문에 DATE2의 열 invoiceduedates의 테이블에 초 기한이없는 경우 뭐죠 여기에 일어나고하는 것은, 때문에 AMOUNT2가 조정 모드 1과 겹쳐지고있다 : 아래

$pay=Invoice::with('invoiceduedates','payments')->where('Eventcode','=',$eventcode)->get(); 
$payment='';$invoicepay=array(); 
foreach($pay as $payble){ 
      $x=0;$due='';$payment='';$i=0; 
      foreach($payble->invoiceduedates as $duedate){ 
         $x++; 
         $due .= $duedate->date.','.$duedate->amount; 
         if($x <= count($payble->invoiceduedates)-1){ 
         $due.=","; 
         } 


      } 

      foreach($payble->payments as $paydate){ 
         $i++; 
         $payment .= $paydate->adjust_mode.','.$paydate->recieved_amount; 
         if($i <= count($payble->payments)-1){ 
         $payment.=","; 
         } 


      } 

      $invoicepay[]= array_merge(explode(',',$due),explode(',',$payment)); 
      unset($due);unset($payment);unset($i);unset($x); 
} 
$export = json_decode(json_encode((array) $invoicepay), true); 

      Excel::create('Data', function($excel) use ($export) 
      { 
       $excel->sheet('Inovice Data', function($sheet) use ($export) 
       { 
        $sheet->fromArray($export); 

        $sheet->cells('A1:AE1', function($cells) 
        { 

           $cells->setBackground('#000000'); 
           $cells->setFontColor('#fff'); 

        }); 
        $sheet->row(1, array(
         'Due Date1','Due Amount1','Due Date2','Due Amount2','AdjustMode1','Rcv Amount1','AdjustMode2','Rcv Amount2' 

        )); 
       }); 
})->download('xlsx'); 

내 엑셀 결과입니다 RCV 금액 1.

실제 엑셀

어떻게 원하는 나는 것을 제어 할 수 empty.How 그 열이 원하는 어떠한 제 기한이없는 경우

| Due Date1 | Due Amount1 | Due Date2 | Due Amount2 | AdjustMode1   | Rcv Amount1 | AdjustMode2   | Rcv Amount2 | 
|------------|-------------|------------|-------------|-----------------------|-------------|-----------------------|-------------| 
| 2016-03-25 | 2000  | 2016-02-29 | 2000  | Overseas Bank Charges | 2000  | Overseas Bank Charges | 2743  | 
| 2016-03-31 | 3750  |   |    | Overseas Bank Charges | 3708  | Overseas Bank Charges | 2750  | 

아 아래 foreach 루프 또는 엑셀의 헤더 부분? 도와주세요 .

감사합니다.

+0

'$ export'에 빈 열이 있습니까? – weigreen

+0

@weigreen 아니 그것은 내가 두 테이블에 여러 행에 데이터를 저장하기 때문에 없어. – 06011991

+0

''$ duedate'와'$ paydate '는 최대 2 줄입니까? – weigreen

답변

1

귀하의 문제는 당신이 $export 빈 공간을 포기하지 않았다이다

그래서 maatwebsite - 엑셀의 -> fromArray() 함수는 문제를 해결할 수있는 하나의

이 코드에 의해 엑셀 하나에 데이터를 저장합니다 : (코드의 일부)

 $x=0;$due='';$payment='';$i=0; 
     foreach($payble->invoiceduedates as $duedate){ 
        $x++; 
        $due .= $duedate->date.','.$duedate->amount; 
        if($x <= count($payble->invoiceduedates)-1){ 
        $due.=","; 
        } 


     } 
     //added 
     if($x == 1){ 
      $due.=",,"; //add two empty column for Due Date2 & Due Amount2 
     } 

     foreach($payble->payments as $paydate){ 
        $i++; 
        $payment .= $paydate->adjust_mode.','.$paydate->recieved_amount; 
        if($i <= count($payble->payments)-1){ 
        $payment.=","; 
        } 


     } 
     //added 
     if($i == 1){ 
      $payment.=",,"; //add two empty column for AdjustMode2 & Rcv Amount2 
     } 
+0

정말 고마워요 !!! 그것은 효과가 ... – 06011991

+0

또 다른 의심의 여지가 있다면 어떻게 세 duedates 동적으로 if 루프에서 조건을 제공하는 것입니다 ... – 06011991

+0

이것은 세 복잡한 일이 있다면, 더 복잡한 것입니다, 당신은 또한 Excel의 첫 번째 행을 변경해야합니다 . 데이터에이 시나리오가 적용됩니까? – weigreen

관련 문제