Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sathishspec's avatar

Laravel-Excel

Hi Experts, in this code, if the candidate satisfies all four conditions. print 4 rows in excel export. but I am getting only one record for each candidate. please help to solve this.

private $data;

use Exportable;
public function __construct(array $data)
{
    $this->data = $data;
}

public function collection()
{
    $search_type = $this->data['search_type'];

    $individuals = Individual::join('users', 'users.id', '=', 'individuals.user_id')->get();
    $companies = Company::join('users', 'users.id', '=', 'companies.user_id')->get();
    $candidates = $individuals->merge($companies);

    $i = 1;  
    $j = 0; 
    $array = array();
    foreach ($candidates as $candidate) {
   
        if($candidate->compliance_visa1 != '')
        {
         
            $candidate->sno = $i++;
            $candidate->name = $candidate->first_name.' '.$candidate->last_name;
            $candidate->doc_type = 'Visa';
            $cur_date = date('d-m-Y');
            $exp_date = date('d-m-Y',strtotime($candidate->compliance_expiry));
            $d1 = date_create($cur_date);
            $d2 = date_create($exp_date);
            $diff = date_diff($d1,$d2);
            $exp_count = $diff->format("%R%a");
            if($exp_count >= +90)
            {
                $exp_status = 'Valid';
            }elseif($exp_count > +30 && $exp_count < +90)
            {
                $exp_status = 'Review and Advise';
            }elseif($exp_count <= +30)
            {
                $exp_status = 'Urgent Action Required';
            }
            $candidate->cer_expiry = $exp_date;
            $candidate->expiry_status = $exp_status;
            $array[$j] = $candidate; 
            $j++;
        }
        if($candidate->nmc_register != '')
        {
            $candidate->sno = $i++;
            $candidate->name = $candidate->first_name.' '.$candidate->last_name;
            $candidate->doc_type = 'NMC';
            $cur_date = date('d-m-Y');
            $exp_date = date('d-m-Y',strtotime($candidate->compliance_nmc_date));
            $d1 = date_create($cur_date);
            $d2 = date_create($exp_date);
            $diff = date_diff($d1,$d2);
            $exp_count = $diff->format("%R%a");
            if($exp_count >= +90)
            {
                $exp_status = 'Valid';
            }elseif($exp_count > +30 && $exp_count < +90)
            {
                $exp_status = 'Review and Advise';
            }elseif($exp_count <= +30)
            {
                $exp_status = 'Urgent Action Required';
            }
            $candidate->cer_expiry = $exp_date;
            $candidate->expiry_status = $exp_status;
            $array[$j] = $candidate; 
            $j++;
        }
        if($candidate->dbs_upload1 != '')
        {
            $candidate->sno = $i++;
            $candidate->name = $candidate->first_name.' '.$candidate->last_name;
            $candidate->doc_type = 'DBS';
            $cur_date = date('d-m-Y');
            $exp_date = date('d-m-Y',strtotime($candidate->dbs_issue));
            $d1 = date_create($cur_date);
            $d2 = date_create($exp_date);
            $diff = date_diff($d1,$d2);
            $exp_count = $diff->format("%R%a");
            if($exp_count >= +90)
            {
                $exp_status = 'Valid';
            }elseif($exp_count > +30 && $exp_count < +90)
            {
                $exp_status = 'Review and Advise';
            }elseif($exp_count <= +30)
            {
                $exp_status = 'Urgent Action Required';
            }
            $candidate->cer_expiry = $exp_date;
            $candidate->expiry_status = $exp_status;
            $array[$j] = $candidate; 
            $j++;
        }
        if($candidate->fitness_upload1 != '')
        {
            $candidate->sno = $i++;
            $candidate->name = $candidate->first_name.' '.$candidate->last_name;
            $candidate->doc_type = 'Fitness to Work';
            $cur_date = date('d-m-Y');
            $exp_date = date('d-m-Y',strtotime($candidate->fitness_issue_date));
            $d1 = date_create($cur_date);
            $d2 = date_create($exp_date);
            $diff = date_diff($d1,$d2);
            $exp_count = $diff->format("%R%a");
            if($exp_count >= +90)
            {
                $exp_status = 'Valid';
            }elseif($exp_count > +30 && $exp_count < +90)
            {
                $exp_status = 'Review and Advise';
            }elseif($exp_count <= +30)
            {
                $exp_status = 'Urgent Action Required';
            }
            $candidate->cer_expiry = $exp_date;
            $candidate->expiry_status = $exp_status;
            $array[$j] = $candidate;
            $j++;
        }
    }

    $this->candidates = $candidates;
    return $candidates;
}
 public function map($report): array
{
    return [
        $report->sno,
        $report->name,
        $report->doc_type,
        $report->cer_expiry,
        $report->expiry_status
    ];
}

public function headings(): array
{
    return [ 
        'S.No',
        'Candidate Name',
        'Document Type',
        'Expiry Date',
        'Status'
    ];
}
0 likes
3 replies
Sinnbeck's avatar

You keep adding to this array but never use it for anything?

$array[$j] = $candidate;

Did you mean to do this?

$this->candidates = $array;
return collect($array);
sathishspec's avatar

@Sinnbeck yes your answer is correct I am getting all data but data duplication is occurring...if one person satisfies the four if conditions I get 4 records but the data was the same in all the 4 records. .....take the last if condition values to all the records

Sinnbeck's avatar

@sathishspec maybe have two arrays. One that you add to each loop but reset after each candidate. And then at the last if, you determine if you want to use it, and merge it with the main array. Or just add the last "if" to the main array

Please or to participate in this conversation.