1

Here's my environment

php : 7.4.2
laravel : 6.14.0
maatwbsite-excel : 3.1.18

Here's the export file

namespace App\Exports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromArray;

class UserReportsExport implements FromArray
{

    use Exportable;

    protected $export_columns;

    function __construct($export_columns)
    {
        $this->export_columns = $export_columns;
    }

    public function array(): array
    {
        return [
            $this->export_columns
        ];
    }
}

Here's the controller

return Excel::download(new UserReportsExport($export_columns), 'Users_Report_' . $todayDate . '.xlsx');

Here is the link that came close to the issue. Based on the solution suggested, I am using return in my controller but still the page is coming blank/empty BUT in my /tmp folder there a .zip like laravel-excel-*** is generated (all it includes are .xml files).

Not sure what I'm missing here. Please suggest. Thanks.

Digvijay
  • 7,836
  • 3
  • 32
  • 53

1 Answers1

0

https://docs.laravel-excel.com/3.1/exports/collection.html#using-arrays looking at it, you need to remove the enclosing brackets in return [ $this->exports_columns ] so it becomes return $this->exports_columns

If it still doesn't work then, you might want to call a ob_end_clean(); before returning the download link. You might have a trailing whitespace after a closing ?> or a var_dump or an echo somewhere messing up your program flow and headers being set. If it starts working with this, check your code, you've got something messing up your headers there by sending early output.

ob_end_clean();
return Excel::download(new UserReportsExport($export_columns), 'Users_Report_' . $todayDate . '.xlsx');

also, if you haven't already, turn on error reporting for everything. It might give something meaningful in return. If you get a blank page that sounds suspicously like an error 500 with error reporting turned off. Check your php logs.

Most common error I can think of is in this case: Cannot modify headers, headers already sent. Please read How can I get useful error messages in PHP?

Tschallacka
  • 27,901
  • 14
  • 88
  • 133