I would like to export my array to an CSV/Excel. I've pretty much followed the documentation but when I'm hitting the download method, I receive an empty / blank page. Any ideas?
Controller
<?php
namespace App\Http\Controllers\Services;
use App\Services\IconParserService;
use Maatwebsite\Excel\Facades\Excel as Excel;
use App\Http\Controllers\Services\IconParserExport;
class IconParserController extends Controller
{
public function exportExcel()
{
$iconParseService = new IconParserService();
$icons = $iconParseService->getIcons();
Excel::download(new IconParserExport($icons), 'export.xlsx');
}
}
Array structure

IconsExport Array
If i do a dd() on the IconsExport i receive data.

dd(new IconParserExport($icons));
IconParserExport
<?php
namespace App\Http\Controllers\Services;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromArray;
class IconParserExport implements FromArray
{
use Exportable;
protected $data;
public function __construct(array $data) {
$this->data = $data;
}
public function array() : array {
return $this->data;
}
}
You have to return the download response, otherwise Laravel doesn't know what to do:
return Excel::download(new IconParserExport($icons), 'export.xlsx');
See the documentation examples, we're we always use return Excel::download
Hey Patrick
Thanks for the quick response. I've switched the Excel download from the service to the Controller. There I forgot tho add the return response. My bad!
thanks very much!
You have to return the download response, otherwise Laravel doesn't know what to do:
return Excel::download(new IconParserExport($icons), 'export.xlsx');See the documentation examples, we're we always use
return Excel::download
But why does it work with query? I do not have to execute this function, when I return query
Excel::download()
And It works and returns right information, but when I change it with Collection or Array, it starts to return an empty blanket
Most helpful comment
Hey Patrick
Thanks for the quick response. I've switched the Excel download from the service to the Controller. There I forgot tho add the return response. My bad!
thanks very much!