I am trying to export a CSV of a cemetery on my website. This is the function right now. However, it is timing out after 10 seconds. Any ideas? To make it more efficient or, simply put, not time out?
public function exportCSV()
{
$fileName = 'cemeteries.csv';
$cemeteries = Cemetery::select(
'name',
'location',
'lot_status',
'gender',
'birth_date',
'death_date',
'veteran',
'holocaust_survivor',
'notes'
)->get();
$headers = [
"Content-type" => "text/csv",
"Content-Disposition" => "attachment; filename=$fileName",
"Pragma" => "no-cache",
"Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
"Expires" => "0"
];
$columns = [
'Name',
'Location',
'Lot Status',
'Gender',
'Birth Date',
'Death Date',
'Veteran',
'Holocaust Survivor',
'Notes'
];
$callback = function() use($cemeteries, $columns) {
$file = fopen('php://output', 'w');
fputcsv($file, $columns);
foreach ($cemeteries as $cemetery) {
$row = [
$cemetery->name,
$cemetery->location,
$cemetery->lot_status,
$cemetery->gender,
$cemetery->birth_date,
$cemetery->death_date,
$cemetery->veteran,
$cemetery->holocaust_survivor,
$cemetery->notes
];
fputcsv($file, $row);
}
fclose($file);
};
return new StreamedResponse($callback, 200, $headers);
}