/
web.php
41 lines (33 loc) · 1.03 KB
/
web.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// example and test
// basic - stream csv to the user
Route::get('csv', function () {
$data = [
[1, 2.1],
[3, "hi, there"],
];
$headers = ['one', 'two'];
$data = array_merge([$headers], $data);
return (new \LaravelCsvGenerator\LaravelCsvGenerator())
->setData($data)
->renderStream();
});
// make csv string and then stream it to the user
Route::get('csv-string', function () {
$httpHeaders = array(
"Content-type" => "text/csv",
"Content-Disposition" => "attachment; filename=file.csv",
"Pragma" => "no-cache",
"Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
"Expires" => "0"
);
$data = [
[1, 2.1],
[3, "hi, there"],
];
$headers = ['one', 'two'];
$data = array_merge([$headers], $data);
$str = (new \LaravelCsvGenerator\LaravelCsvGenerator())
->setData($data)
->toString();
return response()->make($str, 200, $httpHeaders);
});