HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/qas.sports-crowd.com/app/Http/Controllers/Exports/ReportResults.php
<?php

namespace App\Http\Controllers\Exports;

use App\HeaderValue;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;

class ReportResults implements FromCollection, WithHeadings, WithEvents
{
    private $formId, $fromDate, $toDate;

    public function __construct($formId, $fromDate, $toDate)
    {
        $this->formId = $formId;
        $this->fromDate = $fromDate;
        $this->toDate = $toDate;
    }

    // set the headings
    public function headings(): array
    {
        $obj = HeaderValue::select('id', 'form_id', 'user_id', 'created_at')
            ->where('form_id', $this->formId)
            ->with('form', 'user', 'user.documentType', 'header_value_details')
            ->get();

        $data = [];
        if (count($obj) > 0) {
            foreach ($obj as $header) {
                $headers = array(
                    __('messages.answers.title_3'),
                    __('messages.answers.title_4'),
                    __('messages.answers.title_21'),
                    __('messages.answers.document_type'),
                    __('messages.answers.document'),
                    __('messages.answers.title_7'),
                );
                if (count($header->header_value_details) > 0) {
                    foreach ($header->header_value_details as $item) {
                        $headers[] = $item->field->name;
                    }
                }
                $data[] = $headers;
                break;
            }
        }
        return $data;
    }

    // freeze the first row with headings
    public function registerEvents(): array
    {
        return [];
    }

    public function collection()
    {
        $fromDate = $this->fromDate;
        $toDate = $this->toDate;
        $obj = HeaderValue::select('id', 'form_id', 'user_id', 'created_at')
            ->where('form_id', $this->formId)
            ->where(function ($query) use ($fromDate, $toDate) {
                if ($fromDate && $toDate) {
                    $query->whereBetween('created_at', [$fromDate . " 00:00:00", $toDate . " 23:59:59"]);
                }
            })
            ->with('form', 'user', 'user.documentType', 'header_value_details')
            ->get();

        $data = [];
        if (count($obj) > 0) {
            foreach ($obj as $header) {
                $headers = array(
                    $header->form->name,
                    $header->user->first_name . ' ' . $header->user->last_name,
                    $header->user->email,
                    $header->user->documentType ? $header->user->documentType->name : '',
                    $header->user->document,
                    \Carbon\Carbon::parse($header->created_at)->format('Y-m-d h:i:s A')
                );
                if (count($header->header_value_details) > 0) {
                    foreach ($header->header_value_details as $item) {
                        $headers[] = $item->value;
                    }
                }
                $data[] = $headers;
            }
        }
        return collect($data);
    }
}