2015-05-19 136 views
3

我試圖將數據導出爲ex​​cel,但似乎無法使其工作。我正在使用: http://www.maatwebsite.nl/laravel-excel/docsLaravel excel傳遞視圖

我正在儘可能地跟蹤文檔。所以我有這個觀點,我試圖在Excel中獲得。該docs告訴我:

加載視圖用於單張紙

$sheet->loadView('folder.view'); 

所以我這樣做,和(當然)我得到一個錯誤說,在視圖中的變量不是因爲我拒絕不會將變量傳遞到視圖中。所以,我再次閱讀文檔和它說:

變量傳遞到視圖

另外,您可以使用它的工作方式相同與Laravel享有與()方法。

以基本with()爲例。

所以現在我有這個在我的控制器功能:

public function something(){ 
$something2=''; 
$something=''; 
Excel::create('Laravel Excel', function($excel) { 

    $excel->sheet('Excel sheet', function($sheet) { 
     $sheet->loadView('something')->with('something',$something) 
            ->with('something2',$something2); 
     $sheet->setOrientation('landscape'); 
    }); 

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

現在我得到的控制器中的錯誤:

不確定的變量$東西

所以我不知道可能會出現什麼問題。我保持樂觀,並認爲它會像$實際上沒有被聲明的東西一樣簡單,所以我從excel導出中刪除了它。然後,它給我的錯誤:

不確定的變量$ something2

這個我知道還有別的東西錯後。因爲根據excel的創建,兩者都沒有被「定義」。所以我嘗試了一些新的東西。在Excel創建中聲明兩個變量。雖然在聲明變量和創建excel之間有更多的代碼。所以這是行不通的。所以代碼是這樣的:

$something2=''; 
$something=''; 
//more code 
if($check==true){ 
Excel::create('Laravel Excel', function($excel) { 

所以我仍然卡住了。

這裏怎麼回事?我誤解了這個excel創作的東西嗎?

+0

自一cloasure嘗試使用Excel ::創建( 'Laravel的Excel',函數($ Excel文件)使用($ something1,$ somethng2) 同去的其他cloasure $ excel- > sheet('Excel sheet',function($ sheet)use($ something1,$ something2) – oBo

回答

2

我認爲這可能是問題,因爲你使用的是外觀,變量在不定義它們的使用的情況下不可訪問。

public function something(){ 

    $something2=''; 
    $something=''; 

    Excel::create('Laravel Excel', function($excel) use ($something, $something2) { 

     $excel->sheet('Excel sheet', function($sheet) use ($something, $something2) { 
      $sheet->loadView('something')->with('something',$something) 
             ->with('something2',$something2); 
      $sheet->setOrientation('landscape'); 
     }); 

    })->export('xls'); 

} 
+0

它給了我一個不同的錯誤:file_get_contents(// code.jquery.com/ui/1.11.4/themes/smoothness/ jquery-ui.css):未能打開流:沒有這樣的文件或目錄 – Loko

+0

我會說這是另一個與此不相關的錯誤 – oBo

+0

等待我會嘗試修復其他錯誤,我會回來給你。 – Loko

0
### Laravel excel passing a view ### 

    For passing a varriable in excel you use laravel eager loading like under code : 

    $productList = Products::selectProductList(); 

      Excel::create('Current Stock Report', function($excel) use ($productList) { 

       $excel->sheet('Current Stock Report', function($sheet) use ($productList) { 
        $sheet->loadView('backend.report_pdf.current_stock_report_download')->with('productList',$productList); 
        $sheet->setOrientation('landscape'); 
       }); 

      })->export('xls');