HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/timentessagaantrouwen.nl/app/UserResponseExport.php
<?php


namespace App;


use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadings;

class UserResponseExport implements FromCollection, WithHeadings, ShouldAutoSize
{

    public function headings(): array
    {
        return [
            'Naam',
            'Categorie',
            'Aanwezig',
            'Aantal personen',
            'Naar ceremonie',
            'Blijft slapen',
            'Allergieën',
            'Straat',
            'Postcode',
            'Woonplaats',
            'Email',
            'Opmerking',
        ];
    }

    public function collection()
    {

        $collection = collect();

        $users = User::with('response')->get();

        $currentCategory = null;

        foreach ($users as $user) {

            if($user->user_type != $currentCategory) {

                $currentCategory = $user->user_type;
                if($currentCategory != null) $collection->push(['', '']);

            }

            if(empty($user->response)) {
                $collection->push([
                    $user->name,
                    $user->user_type,
                ]);
            }

            if(!empty($user->response)) {
                $collection->push([
                    $user->name,
                    $user->user_type,

                    $user->response->present,
                    $user->response->present_persons,
                    $user->response->at_ceremony,
                    $user->response->stay_sleeping,
                    $user->response->has_allergies,
                    $user->response->street,
                    $user->response->postal,
                    $user->response->city,
                    $user->response->email,
                    $user->response->form_message,
                ]);
            }
        }

        return $collection;
    }

}