Skip to content

Instantly share code, notes, and snippets.

@opejovic
Last active February 2, 2024 07:41
Show Gist options
  • Save opejovic/bf659e9f3ace320639665fb8439b364b to your computer and use it in GitHub Desktop.
Save opejovic/bf659e9f3ace320639665fb8439b364b to your computer and use it in GitHub Desktop.
generating x amount of rows
Route::get('/sample-csv-generation/{numberOfRows}', function (Request $request, $numberOfRows) {
$faker = Faker\Factory::create();
$numberOfRows = (int) $numberOfRows;
// Define the headers for the CSV file
$headers = [
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="filename.csv"',
];
// Create a response using the stream method
return response()->stream(function () use ($faker, $numberOfRows) {
ob_end_clean();
$output = fopen('php://output', 'w');
// Write the CSV header
fputcsv($output, [
'Col 1 name', 'Col 2 name', 'col 3 name',
]);
// Generate and write the CSV rows
foreach (range(1, $numberOfRows) as $i) {
$row = [
// col 1 value,
// col 2 value,
// col 3 value,
];
fputcsv($output, $row);
}
fclose($output);
}, 200, $headers);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment