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