I am trying to export a database table using Laravel as a csv file. I have written down this code. I use it recently worked.But it is not working now.it's seems that everything ok .I do not know fix it
laravel 5.5 php 7 this is my button
<form action="{{route('manage.download')}}" method="POST" enctype="multipart/form-data">
download
this is my class
class csv{
public function download($list, $header, $filename, $quot=true)
{
if (count($header) > 0) {
array_unshift($list, $header);
}
$stream = fopen('php://temp', 'r+b');
foreach ($list as $row) {
$this->_fputcsv($stream, $row,$quot);
}
rewind($stream);
//$csv = str_replace("
", "
", stream_get_contents($stream));
$csv = stream_get_contents($stream);
$headers = array(
'Content-Type' => 'text/csv',
'Content-Disposition' => "attachment; filename=$filename",
);
return \Response::make($csv, 200, $headers);
}
private function _fputcsv($fp, $fields, $quot) {
$tmp = array();
foreach ($fields as $value) {
if($quot){
$value = str_replace('"', '""', $value);
$tmp[]= '"'.$value.'"';
}
else{
$tmp[]= $value;
}
}
$str = implode(',', $tmp);
$str .= "
";
fputs($fp, $str);
}
}
this is my download function
public function csvdownload()
{
//data
$data = [
0 => [
'id' => '1',
'name' => 'test1',
],
1 =>[
'id' => '2',
'name' => 'test2',
],
....
];
//CSV header
$header = ['id', 'name'];
//filename
$filename = Carbon::now()->format('YmdHis') . ".csv";
//class
CSV::download($data, $header, $filename, true);
}