不将整个表下载为.csv文件只导出10条记录(laravel 5.3)

时间:2016-11-15 18:53:02

标签: php mysql laravel csv export

我正在努力导出数据为csv它只下载前9个记录总共10个在excel 1st上是标题和9个记录然后我想从Db导出整个表不知道这里出错的是下面的方法

public function ExportAll(){
      $student = Students::all();
$temp = tmpfile();
$heading = array('Name', 'Group');
fputcsv($temp, $heading);
foreach($student as $row) {
    fputcsv($temp, array($row['name'],$row['group']));
    }
    fseek($temp, 0);
        echo fread($temp, 1024);
        header('Content-Type: text/csv; charset=utf-8');
        header('Content-Disposition: attachment; filename=Student.csv' );
        fclose($temp);
        die;
    }

它下载只有一行我想下载整个表使用这种方法 请帮忙解决 路线是

Route::get('/student/ExportAll', 'StudentController@ExportAll');

这里我有链接导出为

<a href="/student/ExportAll">Export</a>

1 个答案:

答案 0 :(得分:0)

每次通过数组都会覆盖$values

$values = [];
foreach($student as $row) {
    $values[] = array($row['name'],$row['group']);
}