仅导出单个记录标题不是数据(laravel 5.3) [英] Export a single record only Headers downing not the data (laravel 5.3)

查看:146
本文介绍了仅导出单个记录标题不是数据(laravel 5.3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从DB导出单个记录。我试图写不同的方法,但失败。

I am trying to export single record from DB. I have tried to write different methods but failed. Now I try this one here, it's just downloading the headers and no data.

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

这里只是给出了表头而不是数据。如何获得所有?

Here it just gives the table head not data below. How can I get all?

推荐答案

UPDATE v3 >

UPDATE v3 according the comments.

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

这篇关于仅导出单个记录标题不是数据(laravel 5.3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆