简体   繁体   English

导出1并将多个导出为.csv(laravel 5.3)

[英]Export 1 and Export Multiple as .csv (laravel 5.3)

I am working on export and I want to use 2 buttons one is for export only one record and other one is to export multiple records which are selected by checkboxes. 我正在进行导出,我想使用2个按钮,一个用于导出仅一个记录,另一个用于导出多个按复选框选择的记录。 I am able to get id's in export method but don't know how to extract data from database and put it in csv file. 我能够在导出方法中获取id,但不知道如何从数据库中提取数据并将其放在csv文件中。

Here is my method (which is working for single record export) . 这是我的方法(用于单个记录导出)

public function getExport($id, Request $request)
{
    $student = Students::find($id);
    $name = $student->name . ".csv";
    $temp = tmpfile();
    $heading = array('Name', 'Group');

    fputcsv($temp, $heading, );

    $values = array($student->name, $student->group);

    fputcsv($temp, $values, );
    fseek($temp, 0);
    echo fread($temp, 1024);
    header('Content-Type: text/csv; charset=utf-8');
    header("Content-Disposition: attachment; filename=" . $name);
    fclose($temp);
    die;
}

link for this method 此方法的链接

<a href="/student/getExport/{{ $stuent->id }}">Export It</a>

Route 路线

Route::get('/student/getExport/{id}', 'StudentController@getExport');

Till here it was for one record export . 直到这里它是一个记录导出

Now for multiple record export I have a JavaScript function which get the IDs which are neededs to be exported. 现在,对于多记录导出,我有一个JavaScript函数,可以获取需要导出的ID。

Link for Multiples Record Export 多重记录导出的链接

<a href="javascript:;" onclick="exportAll();"> Export Students  </a>

javascript method javascript方法

function exportAll() {
    var students = $('input:checkbox:checked').map(function () {
        return this.value;
    }).get();
    $.ajax({
        type: "GET",
        url: '/student/getExport/' + students,
        data: {
            ids: students
        },
        success: function (result) {
            if (result == 'export') {
                window.location.href = "/smtp";
            }
        }
    });
}

Through this method I'm getting the id's of selected record in getExport method. 通过这种方法,我得到了getExport方法中所选记录的id。

Hint This code is working for both single and multiple records delete but don't know how to use it for export as well. 提示此代码适用于单个和多个记录删除,但不知道如何使用它进行导出。

public function destroy($id, Request $request)
{
    if (empty($request->input('ids')))
    {
        $student = Students::findOrFail($id);
        $student->delete();
    }
    else
    {
        $ids = $request->input('ids');
        $Student = Students::whereIn('id', $ids);
        $student->delete();
        session()->flash('msg', trans('Deleted successfully.'));
    }
    return 'delete';
}

Please help me how to achive export for multiple records in same method getExport . 请帮助我如何以相同的方法getExport实现多个记录的导出。

Thanks 谢谢

You have to use get methord insted of find , and then you have to loop it to append in your csv file. 你必须使用get methord insted find ,然后你必须循环它以附加在你的csv文件中。

Here is the code, which I have modified from my previous answer . 这是代码,我从之前的回答中修改过。

public function getExport($id)
{
    $data = Students::whereIn('id', explode(',', $id))
        ->get();
    $filename = "students.csv";
    $handle = fopen($filename, 'w+');
    fputcsv($handle, array('name', 'class', 'section'));
    foreach ($data as $student)
    {
        fputcsv($handle, array($student->name, $student->class, $student->section));
    }
    fclose($handle);

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM