How to export excel in laravel and which library is best for laravel 5.5.
Answer (1)
First thing need to do i.e install Laravel maatwebsite package for use Excel facade.
Just run command as below in composer or girhub bash
composer require maatwebsite/excel
Then open config/app.php
return [....
'providers' => [....
Maatwebsite\Excel\ExcelServiceProvider::class,
],
'aliases' => [ ....
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
],
....
And at last here is code for export
create new method for controller.
/**
* Create a new controller instance.
*
* @return void
*/
public function exportFile($type){
$products = Product::get()->toArray();
return \Excel::create('download_excel_demo', function($excel) use ($products) {
$excel->sheet('sheet name', function($sheet) use ($products)
{
$sheet->fromArray($products);
});
})->download($type);
}