Hi.
I am use Excel::download($contract->Collection() , 'contract.xlsx'); to export data.
It create file.xlsx but empty.
my collection like this : object(IlluminateDatabaseEloquentCollection)#785 (1) { ["items":protected]=> array(3) .
php :7.1.15
laravel : 5.5
maatwebsite/excel : ^3.0.
Anyone help me ?
For anybody else running into this, please check the documentation (https://laravel-excel.maatwebsite.nl/docs/3.0/export/basics) for the correct syntax of exporting a collection.
For anyone who was trying to avoid defining an export class for each model to be exported like me, using an anonymous class is a good way to do this:
return Excel::download(new class($collection) implements FromCollection{
public function __construct($collection)
{
$this->collection = $collection;
}
public function collection()
{
return $this->collection;
}
},rtrim($this->excelFileName, ".xlsx").".xlsx");
I am facing same issue, I am passing collection and string of file name to download method and it downloads an empty excel. My code:
return Excel::download($prov_addresses, 'addresses.xlsx');
where $prov_addresses is a collection of arrays:
Collection {#193 â–¼
#items: array:50 [â–¶]
}
It downloads excel but empty. I have checked documentation but no help.
Have you read my comment? See the documentation for the correct usage please.
I couldn't find my mistake, so I downgraded to version 2.1 and used that old syntax. It worked.
@nitinraghav
The only way you can export is via FromCollection concern:
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
class InvoicesExport implements FromCollection
{
public function collection()
{
return Invoice::all();
}
}
Define your collection that you want to export there then export that:
Excel::download(new InvoicesExport, 'invoices.xlsx');
If you try to export any other way you will get an empty set.
@surgiie am getting "Call to a member function each() on array" error when trying to use your advice
could you help me out on the variables you used.
@isaacpitwa can you post your code snippet?
Be sure you're passing in a collection. Sounds like you're passing in an array.
@surgiie it was an array indeed . thanks, the code works fine now
How do you solve it? @isaacpitwa ?
Most helpful comment
For anyone who was trying to avoid defining an export class for each model to be exported like me, using an anonymous class is a good way to do this: