Skip to content

Instantly share code, notes, and snippets.

@btxtiger
Last active December 25, 2023 23:35
Show Gist options
  • Save btxtiger/902d2742112a29d7ca5bb655b0d0eaaf to your computer and use it in GitHub Desktop.
Save btxtiger/902d2742112a29d7ca5bb655b0d0eaaf to your computer and use it in GitHub Desktop.
Laravel download CSV string as file
<?php
$csv = \App\Services\DataExportService::createCsvExport();
$BOM = "\xEF\xBB\xBF";
return response()->streamDownload(
function () use ($BOM, $csv) {
echo $BOM . $csv;
},
'data-export.csv',
[
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename="data-export.csv',
'Content-Encoding: UTF-8',
],
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment