Laravel-excel: [QUESTION] Excel::download() is returning an empty/blank page

Created on 17 Jul 2019  路  3Comments  路  Source: Maatwebsite/Laravel-Excel

Prerequisites

Versions

  • PHP version: 7.3
  • Laravel version: 5.8.*
  • Package version: 3.1

Description

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?

Additional Information

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
image

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

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;
    }
}

question

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!

All 3 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

octoxan picture octoxan  路  3Comments

contifico picture contifico  路  3Comments

matthewslouismarie picture matthewslouismarie  路  3Comments

gamevnlc picture gamevnlc  路  3Comments

dr3ads picture dr3ads  路  3Comments