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

noblemfd's avatar

How to get resumption date in Laravel Maatwebsite import

I am using Laravel-5.8 and Maatwebsite for Excel File upload:

class FirstLeaveSheetImport implements OnEachRow, WithHeadingRow
{
    public function onRow(Row $row)
    {   
        $rowIndex = $row->getIndex();

        if($rowIndex >= 200)
            return; // Not more than 200 rows at a time

        $row = $row->toArray();

        $leave = [
            'employee_id'           => $row['employee_id'],
            'leave_type_id'         => $row['leave_type_id'],
            'commencement_date'     => $this->transformDate($row['commencement_date']),
            'resumption_date'       => $resumptionDate,
            'no_of_days'            => 'no_of_days',
        ];

        $tb = create(HrLeaveRequest::class, $leave);

    public function transformDate($value, $format = 'Y-m-d')
    {
        try {
            return \Carbon\Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value));
        } catch (\ErrorException $e) {
            return \Carbon\Carbon::createFromFormat($format, $value);
        }
    }            
}

I have these models

class HrHolidayDate extends Model
{
    protected $fillable = [
                  'holiday_date',
              ];
}

class HrLeaveRequest extends Model
{
    protected $fillable = [
                  'id',
                  'employee_id',
                  'leave_type_id',
                  'commencement_date',
                  'resumption_date',
                  'no_of_days',
              ];
}

The imported excel file goes to the HrLeaveRequest model.

The user is only allowed to input the commencement_date and no_of_days in the excel file.

I want the import to use the commencement_date and no_of_days entered by the user to get the resumption_date. The application should add the no_of_days to the commencement_date and substract weekend and also holidays (HrHolidayDate) from it before import.

I have:

'resumption_date'       => $resumptionDate,

and want to get this:

$resumptionDate

How do I achieve this?

Thanks

0 likes
1 reply
Snapey's avatar
Snapey
Best Answer
Level 122

start by isolating to a function, and passing it the values it needs to work

            'resumption_date'       => $resumptionDate($row['commencement_date',$row['days'])

and create the function


private function($commencement, $days)
{

	// calculate the end date according to your logic

	return $resumptionDate;

}

Then you only have to concentrate on the one function and can ignore the rest of the stuff. The responsibility of this function is to return this one value.

Please or to participate in this conversation.