ruslanashaari
9/18/2017 - 3:36 PM

ExcelExport.php

<?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'));