I'm a newbie in Laravel, but I'm using laravel's Maatwebsite\Excel Library v3 to export excel. But I'm having some problems exporting my array data.
here is my code
<?php
namespace App\Exports;
use App\Team;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;
class RegisteredMemberExport implements FromCollection, WithHeadings
{
use Exportable;
public function collection()
{
$data = Team::where('reg', 1)->get();
return collect([
[
'name' => $data->name,
'email' => $data->email
]
]);
}
public function headings(): array
{
return [
'name',
'email'
];
}
}
the collect should be
return collect
([
[
'name' => 'Povilas',
'email' => '[email protected]'
],
[
'name' => 'Taylor',
'email' => '[email protected]'
]
]);
I can't use a loop inside the collect method return. Can I please have some help?
You can directly filter the values you need from your Eloquent model using a list of attributes as a parameter for the get method.
$data = Team::where('reg', 1)->get(['name', 'email']);
return collect($data->toArray());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With