etalon_backend/plugins/vdomah/excel/classes/ExportExample.php

49 lines
1.2 KiB
PHP
Raw Normal View History

2021-12-22 08:07:43 +00:00
<?php namespace Vdomah\Excel\Classes;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Events\AfterSheet;
class ExportExample implements FromCollection, WithHeadings, WithEvents
{
// set the headings
public function headings(): array
{
return [
'Company name', 'Flyer name', 'Co Company', 'Post Code', 'Online invitation', 'Pending'
];
}
// freeze the first row with headings
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$event->sheet->freezePane('A2', 'A2');
},
];
}
// get the data
public function collection()
{
//example returning all users
//return User::all();
//example returning faker data
$data = [];
if (class_exists('\Faker\Factory')) {
$faker = \Faker\Factory::create();
$limit = 10;
for ($i = 0; $i < $limit; $i++) {
$data[] = [$faker->name, $faker->word, 'N', $faker->postcode, $faker->word, $faker->word];
}
}
return collect($data);
}
}