导出一条记录,只导出标题,不导出数据(laravel 5.3)


Export a single record only Headers downing not the data (laravel 5.3)

我试图从DB导出单个记录。我试着写不同的方法,但失败了。现在我试试这个,它只下载头文件,没有数据。

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);
}

这里它只给出表头而不是下面的数据。我怎样才能得到全部?

根据注释更新 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);
    }