I'm trying to batch generate several CSV file, so I've made a helper class. I have a common private function CSVLoop which is working fine with my makeResponse() method, but I'm struggiling to understand the parameters I need to be supplying to ->putFileAs():
namespace App\Helpers;
use Illuminate\Support\Collection;
use Storage;
use Symfony\Component\HttpFoundation\StreamedResponse;
class CSV
{
private static function CSVLoop($file, $columns, $data) :void
{
fputcsv($file, array_keys($columns));
$i = 1;
foreach ($data as $item) {
$row = [];
foreach ($columns as $column => $field) {
if(is_null($field)) $row[$column] = "";
else $row[$column] = $column !== "#" ? $item[$field] : $i;
}
fputcsv($file, array_values($row));
$i++;
}
fclose($file);
}
public static function makeLocal(String $fileName, String $path, Array $columns, Array | Collection $data) :bool
{
$csvFile = tmpfile();
$csvPath = stream_get_meta_data($csvFile)['uri'];
$file = fopen($csvPath, "w");
CSV::CSVLoop($file, $columns, $data);
Storage::disk("public")->putFileAs($csvPath, $file, $fileName);
return true;
}```