2016-11-15 58 views
0

我試圖從數據庫導出單個記錄。我試圖寫出不同的方法,但失敗了。現在我在這裏試試這個,它只是下載頭文件而沒有數據。僅導出單個記錄標題不是數據(laravel 5.3)

public function getExport($id) { 
    $student = Students::find($id); 
    $filename = "students.csv"; 
    $handle = fopen($filename, 'w+'); 
    fputcsv($handle, array('name', 'class', 'section')); 

    foreach($student as $row) { 
     fputcsv($handle, array($row['name'], $row['class'], $row['section'])); 
    } 

    fclose($handle); 

    $headers = array(
     'Content-Type' => 'text/csv', 
    ); 

    return Response::download($filename, 'Students.csv', $headers); 
} 

這裏它只是給出表頭而不是下面的數據。我如何獲得全部?

+0

你有沒有嘗試使用[這](http://www.maatwebsite.nl/laravel-excel/docs/import)? – prateekkathal

回答

1

UPDATE v3 根據評論。

public function getExport($id) 
    { 
     $student = Students::find($id); 
     $filename = $student->name . ".csv"; 
     $handle = fopen($filename, 'w+'); 
     fputcsv($handle, array('name', 'class', 'section')); 
     fputcsv($handle, array($student->name, $student->class, $student->section)); 

     fclose($handle); 

     $headers = array(
      'Content-Type' => 'text/csv', 
     ); 

     return \Response::download($filename, $filename, $headers); 
    } 
+0

Thannks親愛的@Raunak Gupta 親愛的我要更新我的問題,請解決問題,我現在有小問題 –

+1

@recoverymen:現在檢查。 –

+0

非常感謝它的工作 還有一個需要你的幫忙,當這個文件下載它的名字應該是學生的名字,如xyz。用於xyz學生的csv和用於abc學生姓名的abc.csv。請爲我做這個 –

相關問題