0

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!

Damon
  • 4,151
  • 13
  • 52
  • 108
  • i think it is good to get data in object format by using get() instead array format?and try this fromArray($result) works for get method.. – RamAnji Sep 16 '17 at 13:08
  • Do you have same key name for each index? and try to dd before passing value to $sheet->fromArray() – Ali Sep 17 '17 at 06:06

2 Answers2

0

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.

Suniti Yadav
  • 393
  • 2
  • 8
0

Try

$sheet->fromArray((array)$result);
nartoan
  • 368
  • 2
  • 9