【问题标题】:Export a single record only Headers downing not the data (laravel 5.3)仅导出单个记录标题而不是数据(laravel 5.3)
【发布时间】:2016-11-15 06:49:04
【问题描述】:

我正在尝试从数据库中导出单个记录。我曾尝试编写不同的方法但失败了。现在我在这里试试这个,它只是下载标题,没有数据。

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

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

【问题讨论】:

  • 您尝试过使用this 吗?

标签: php laravel csv export


【解决方案1】:

更新 v3 根据 cmets。

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

【讨论】:

  • 谢谢亲爱的@Raunak Gupta 亲爱的我要更新我的问题请解决我现在有小问题的问题
  • @recoverymen:现在检查。
  • 非常感谢它的工作还有一个需要你的帮助,当这个文件下载时,它的名字应该是学生的名字,意思是xyz。 csv 代表 xyz 学生,abc.csv 代表 abc 学生姓名。请为我做这件事
  • 这是返回学生姓名$student->name 吗?
  • 是的,它会返回,但问题是如何使用该名称设置扩展名 .csv
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多