Следующая функция работает нормально, она имеет маршрутизатор, как:
Route::get('/export/{table_name}', 'Db2CSVController@export')
public function export(Request $request, $table_name){
$db = env("DB_DATABASE");
DB::statement( DB::raw('set session group_concat_max_len = 1000000;'));//useful when you have hundereds of columns
$header_csv = DB::table('INFORMATION_SCHEMA.COLUMNS')
->selectRaw("GROUP_CONCAT(CONCAT('\"',COLUMN_NAME,'\"')) as header")
->whereRaw("TABLE_NAME = '$table_name'")
->whereRaw("TABLE_SCHEMA = '$db'")
->orderBy("ORDINAL_POSITION")
->first()->header;
$data = DB::table($table_name)->get()->toArray();
$filename = "$table_name".".csv";
$filename_to_downlaod = $filename;
$handle = fopen($filename, 'w+');
fputs($handle,$header_csv . "\n");
foreach($data as $row) {
fputcsv($handle, (array)$row);
}
fclose($handle);
$headers = array(
'Content-Type' => 'text/csv',
);
return Response::download($filename, $filename_to_downlaod, $headers);
}