Hey BobbyBouwmann,
I like your answer, thanks for it.
I noticed I did not fully copy the code of the export.
<?php
namespace App\Exports;
use App\Time;
use Carbon\Carbon;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
class Times2Export implements FromCollection, WithMapping, WithHeadings, ShouldAutoSize, WithColumnFormatting
{
public function collection()
{
return Time::all()->sortBy("car_id");
}
public function headings(): array
{
return [
'Dossiernummer',
'Auto',
'Taak',
'Werknemer',
'Starttijd',
'Stoptijd',
'Duur',
];
}
/**
* @var Time $time
*/
public function map($time): array
{
$dt = Carbon::parse($time->stop)->diffInSeconds(Carbon::parse($time->start));
$duur = (is_null($time->stop)) ? 0 : $dt;
return [
$time->car->dossiernr,
$time->car->brand . " " . $time->car->type . " (" . $time->car->license . ")",
$time->task->name,
$time->employee->name,
$time->start,
$time->stop,
'=' . $duur . '/86400',
];
}
public function columnFormats(): array
{
return [
'G' => NumberFormat::FORMAT_DATE_TIME4,
];
}
}
When looking on the page you pasted, I see this:
namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromCollection;
class InvoicesExport implements FromCollection
{
public function collection()
{
return new Collection([
[1, 2, 3],
[4, 5, 6]
]);
}
}
I cannot really find what should be the [1,2,3], [4,5,6] in my case then.
Sorry I'm a newbie and maybe it is a stupid question, but a small hint could probably help.
Thanks in Advance,
Davy