Skip to content

Instantly share code, notes, and snippets.

@ruslanashaari
Created October 3, 2017 08:05
Show Gist options
  • Save ruslanashaari/8acaef0c707c7f4417bbdc95afa59f27 to your computer and use it in GitHub Desktop.
Save ruslanashaari/8acaef0c707c7f4417bbdc95afa59f27 to your computer and use it in GitHub Desktop.
<?php
$results = $this->resource->get(array('id','column1','column2','column3', 'etc'));
$fileName = 'my-export-'.Carbon::now()->timestamp;
$export = $this->excel->create($fileName, function($excel) use ($results) {
$excel->setTitle('Our new awesome title')
->setCreator('Maatwebsite')
->setCompany('Maatwebsite')
->setDescription('A demonstration');
$excel->sheet('Sheetname', function($sheet) use ($results) {
foreach($results as $index => $result){
//Increment Overide Zero-based Index
$index++;
//Get Field Names & Data as Array
$attributes = $result->getAttributes();
//Set Field Names or Data for Row
$sheet->row($index, ($index == 1 ? array_keys($attributes) : $attributes));
}
$sheet->setColumnFormat(array(
'A' => '0', //id column is integer (column1)
'B' => '0', //user_id column is integer (column2)
));
});
});
return response()->download($export->download('xls'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment