Skip to content

Instantly share code, notes, and snippets.

@ChVuagniaux
Last active March 9, 2018 07:53
Show Gist options
  • Save ChVuagniaux/16cba477e5748ef3dc591a4d88b001a6 to your computer and use it in GitHub Desktop.
Save ChVuagniaux/16cba477e5748ef3dc591a4d88b001a6 to your computer and use it in GitHub Desktop.
CSV export for Excel
<?php
use League\Csv\Writer;
use League\Csv\AbstractCsv;
$csv = Writer::createFromFileObject(new \SplTempFileObject());
$csv->setDelimiter(';');
$csv->setOutputBOM(AbstractCsv::BOM_UTF8); // compatibility with Excel
$csv->insertOne(['1', 'é', 'à']);
$csv->output('my-file.csv');
exit();
<?php
use League\Csv\Writer;
use League\Csv\AbstractCsv;
$tmpFile = storage_path('temp/commandes-' . uniqid() . '.csv', 'w+');
file_put_contents($tmpFile, chr(239) . chr(187) . chr(191));
$products = $this->produits->map(function ($item) {
return [
'produit' => $item->nom,
];
});
$csv = Writer::createFromPath($tmpFile, 'a');
$csv->setDelimiter(';');
$csv->setOutputBOM(Writer::BOM_UTF8); // compatibility with Excel
$csv->insertOne([
'Produit',
]);
$csv->insertAll($products);
Mail::send('commande-admin', ['commande' => $this], function ($message) use ($tmpFile) {
$message->to('[email protected]');
$message->attach($tmpFile, ['as' => 'detail_commande_' . date('Y-m-d-His') . '.csv']);
});
unlink($tmpFile);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment