Last active
March 9, 2018 07:53
-
-
Save ChVuagniaux/16cba477e5748ef3dc591a4d88b001a6 to your computer and use it in GitHub Desktop.
CSV export for Excel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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