Why is my Laravel Export returning a blank sheet?

2.6k views Asked by At

I am using Laravel Export to export an excel file of some data. When I create my query, and use dd() to check what the data look like, everything looks good. For example:

dd($result);

array:510 [▼
    0 => array:24 [▼
        "abc" => "123"
        "foo" => "bar"
        "key" => "value"
        ...
        "pivot" => array:4 [ …4]
    1 => array:24 [▼
        ...
 ]

This is what my export method looks like:

Excel::create('my-file', function ($excel) use ($result) {
    $excel->sheet('Page1', function ($sheet) use ($result) {
        $sheet->fromArray(array($result));
    });
})->export('xlsx');

The above generates a completely blank excel file.

I am not sure how else to troubleshoot this. It seems I am passing in good data, but getting an empty sheet back.

Grateful for any suggestions!

2

There are 2 answers

0
Suniti Yadav On

Try this -

Excel::create('my-file', function ($excel) {
    $excel->sheet('Page1', function ($sheet) {
        $sheet->fromArray($result);
    });
})->export('xlsx');

Reffer this link as well - http://www.maatwebsite.nl/laravel-excel/docs/export for Creating a sheet from an array

Hope this will help you.

0
nartoan On

Try

$sheet->fromArray((array)$result);