2016-05-01 78 views
0

我想所有記錄從視圖在我的控制器使用maatweb包laravel Excel導出所有記錄失敗

導出到Excel表,我用下面的函數

public function exportAll() 
    { 
Excel::create('POs', function ($excel) 

     { 
      $excel->setTitle('Pos'); 
      $excel->sheet('POs', function ($sheet) 

      { 

       $pos = PO::all(); 
       $arr =array(); 

           foreach($pos as $po) 
           { 
            foreach ($po->mr as $m) 
             foreach ($po->suppliers as $supplier) 

             { 
              $data = array($m->mr_no, $po->po_no, $po->po_subject, 
               $po->po_issued, $po->po_total_cost, $po->po_currency, 
               $po->po_purchase_method, $po->po_payment_method, 
               $po->po_delivery_method, $po->po_confirmation, 
               $po->po_loaded_on_ideas, $supplier->vname, 
               $po->po_loaded_on_ideas, $po->po_approved_on_ideas, 
               $po->memo_to_fin, $po->po_delivery_date, 
               $po->po_mr_received_date, $po->po_mrr_received_date, 
               $po->po_invoice_received_date, $po->po_penalty, 
               $po->po_cover_invoice, $po->po_completed 

              ); 
              array_push($arr, $data); 


             } 
           } 

            $sheet->loadView('pos.pos_all_template')->with('pos',$pos); 
       // 

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

,並在路由文件我用
路線::得到( 'po_s/exportall', 'POsController @ exportAll');

在我看來,文件

@foreach($pos as $p) 
@foreach($p->mr as $m) 
@foreach($p->suppliers as $s) 

<tr style="color:rgb(0, 0, 0);" align="center" > 

    <th align="center" > {{ $m['mr_no'] }} </th> 
    <th align="center" > {{ $p['po_no'] }} </th> 
    <th align="center" > {{ $p['po_subject'] }} </th> 
    <th align="center" >{{ $p['po_issued'] }} </th> 
    <th align="center" >{{ $s['vname'] }} </th> 
    <th align="center" >{{ $p['po_total_cost'] }} </th> 
    <th align="center" >{{ $p['po_currency'] }} </th> 
    <th align="center" >{{ $p['po_purchase_method'] }} </th> 
    <th align="center" >{{ $p['po_payment_method'] }} </th> 
    <th align="center" >{{ $p['po_delivery_method'] }} </th> 
    <th align="center" >{{ $p['po_delivery_date'] }} </th> 
    <th align="center" >{{ $p['po_loaded_on_ideas'] }} </th> 
    <th align="center" >{{ $p['po_mr_received_date'] }} </th> 
    <th align="center" >{{ $p['po_mrr_received_date'] }} </th> 
    <th align="center" >{{ $p['po_invoice_received_date'] }} </th> 
    <th align="center" >{{ $p['po_penalty'] }} </th> 
    <th align="center" >{{ $p['po_cover_invoice'] }} </th> 
    <th align="center" >{{ $p['po_completed'] }} </th> 



</tr> 
@endforeach 
@endforeach 
@endforeach 

的問題,我接收空

excel文件沒有任何數據

回答

0

第一次嘗試這個 - >dd($pos = PO::all(););enter code here DD($ POS); 如果你得到輸出,那麼試試你的for-each循環你使用dd();時得到的結果。如果你得到的數據則創建 Excel工作表的問題檢查下面的代碼如何即時生成用紙

public function createDocs() 
     { 
    \Excel::create('JOBS', function($excel) { 
    $excel->sheet('2015', function($sheet) { 
     $jobs = DB::table('jobs')->get(); 
     $data = array('JOB ID','CONSIGNEE CODE','CONSIGNEE NAME','CONSIGNEE VAT NO','DESCRIPTION','VESSEL & VOYAGE','INVOICE VALUE','ETA/ATA DATE','B/L or AWB NUMBER','COMPLETE DATE','DELIVERY INVOICE VALUE','DUTY AMOUNT','TERMINAL','PAYMENT','INCOTERMS','COM INVOICE NO','COM INVOICE DATE','COM INVOICE VALUE','BANK','BANKREFNO','ORIGIN','EXPORT','TRADING','TOTAL EXPENSES','PROFIT'); 
    $sheet->fromArray(array($data),null,'A1',false,false); 

foreach($jobs as $row){ 
    $sum = DB::table('charges')->where('job_id', $row->id)->sum('amount'); 
    $consignee = DB::table('consignees')->where('id', $row->id)->first(); 
         $data=array($row->id,$consignee->code,$consignee->name,$consignee->vat_num,$row->description,$row->vessel, 
          $row->invoice_value,$row->eta_date,$row->awb_num,$row->complete_date,$row->del_inv_val,$row->duty_amount, 
          $row->terminal,$row->payment,$row->incoterm,$row->com_inv_no,$row->com_inv_date,$row->com_inv_value,$row->bank,$row->bank_ref_no, 
          $row->country_origin,$row->country_export,$row->country_trading,$sum,$row->invoice_value-$sum); 
         $sheet->fromArray(array($data),null,'A1',false,false); 
}});})->download('csv');} 

,並使用這個鏈接http://www.maatwebsite.nl/laravel-excel/docs/export

+0

它實際上與你的代碼工作,但我需要加載從視圖文件 – Hussein

+0

@Hussein的意思是?您的數據庫中的數據權利,所以你必須與你的數據庫交談?沒有查看?你解決了嗎? – Hamelraj