Created
December 17, 2019 15:52
-
-
Save grandmanitou/d8ccc8141eb860385c2911a7ca954556 to your computer and use it in GitHub Desktop.
Laravel Export Trait for Box/Spout
This file contains hidden or 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 | |
namespace App\Traits; | |
use Box\Spout\Writer\Common\Creator\WriterEntityFactory; | |
trait Export | |
{ | |
public static function export($target, $items) | |
{ | |
$writer = WriterEntityFactory::createXLSXWriter(); | |
$writer->openToFile($target); | |
// Columns | |
$cells = array_keys($items->first()->toArray()); | |
$row = WriterEntityFactory::createRowFromArray($cells); | |
$writer->addRow($row); | |
// Rows | |
foreach($items as $item) { | |
$cells = array_values($item->toArray()); | |
$row = WriterEntityFactory::createRowFromArray($cells); | |
$writer->addRow($row); | |
} | |
$writer->close(); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment