Hi there,
I wonder if anyone can help me.
I am working on creating an export to CSV function for any collection I pass to it.
I have created the following class.
<?php
namespace App\Exporter;
class Exporter
{
public function exportToCsv($collection)
{
$first = $collection->first();
$output = implode(", ", array_keys($first['attributes']))."\n";
foreach ($collection as $row)
{
$output .= implode(", ",$row['attributes'])."\n";
}
$headers = [
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="export.csv"',
];
return \Response::make($output, 200, $headers);
}
}
This works well if there are no commas, but one field on say the Users table contains a description where the user can easily enter commas. These commas are breaking the layout of my csv export. I know I need to escape the commas I think by wrapping each field in double quotes but can't figure out how to achieve this with the implode function.
Any help appreciated!