Home > Back-end >  Showing error "Invalid date value" while importing excel file in Laravel 7
Showing error "Invalid date value" while importing excel file in Laravel 7

Time:06-16

I want to import data from an Excel file. While inserting data, I want to calculate some value depending on the requested Excel file. But the time difference is not returning the correct date format.

Controller:

public function import(Request $request){
  $request->validate([
    'import_file'=>'required'
   ]);
   Excel::import(new ImportAttendanceExcel,request()->file('import_file'));
   return back()->with('message','XL File Import Successfully!');

 }

Model:

class ImportAttendanceExcel implements ToModel, WithStartRow
{
    public function model(array $row)
    {   $id = 1;
        $settings = Setting::find($id);
        $open =  Carbon::parse($settings->opening_time);
        $end =   Carbon::parse($settings->ending_time);
        $date = Carbon::parse(Date::excelToDateTimeObject ($row[2]));
        $user = $row[0];
        $exist_attd = Attendance::where('user_id', $user)->where('attendance_date', $date)->get()->toArray();
            if($row[0] != null){
                if(empty($exist_attd)){
                    $attendances = new Attendance;
                    $attendances->user_id = $row[0];
                    $attendances->created_by = Auth::user()->id;
                    $attendances->attendance_date =Carbon::parse(Date::excelToDateTimeObject ($row[2]));
                    $attendances->attendance_status  = $row[3];
                    $attendances->check_in = Carbon::parse(Date::excelToDateTimeObject ($row[4]));
                    $attendances->check_out = Carbon::parse(Date::excelToDateTimeObject ($row[5]));
                    $csv_check_in = Carbon::parse(Date::excelToDateTimeObject($row[4]));
                    $csv_check_out = Carbon::parse(Date::excelToDateTimeObject($row[5]));
                    if( $csv_check_in > $open){
                        $late_count = $csv_check_in->diff($open)->format('%H:%I:%S');
                        $attendances->late = $late_count;
                    }
                    if( $csv_check_out < $end){
                        $early_count = $csv_check_out->diff($end)->format('%H:%I:%S');
                        $attendances->early = $early_count;
                    }

                 $attendances->save();
                }

            }
    }


    public function startRow(): int
    {
        return 2;
    }

}

Return Value with error:

(SQL: insert into `attendances` (`user_id`, `created_by`, `attendance_date`, `attendance_status`, `check_in`, `check_out`, `early`, `updated_at`, `created_at`) values (2, 1, 44713, 1, 0.42361111111111, 0.77430555555556, 23:59:59, 2022-06-14 15:12:47, 2022-06-14 15:12:47))

Excel File:

id date status check-in check-out
1 2022-06-10 1 10:15:00 18:00:00

Here the date format and the calculation is not working properly. How can I solve this?

CodePudding user response:

You cant just directly throw date from excel to DB. You need to format it first. Since laravel-excel is built from phpSpreadsheet. You can import Date Class

use \PhpOffice\PhpSpreadsheet\Shared\Date;

// Rest of your code

$date = Date::excelToDateTimeObject($yourExcelDate);

// Rest or your code

From the PHPSpreadsheet docs:

\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($excelDate)

Converts a date from an Excel date/timestamp to return a PHP DateTime object.

CodePudding user response:

My problem is solved by this :

class ImportAttendanceExcel implements ToModel, WithStartRow
    {
        public function model(array $row)
        {   $id = 1;
            $settings = Setting::find($id);
            $open =  Carbon::parse($settings->opening_time);
            $end =   Carbon::parse($settings->ending_time);
            $date = Carbon::parse(Date::excelToDateTimeObject ($row[2]));
            $user = $row[0];
            $exist_attd = Attendance::where('user_id', $user)->where('attendance_date', $date)->get()->toArray();
                if($row[0] != null){
                    if(empty($exist_attd)){
                        $attendances = new Attendance;
                        $attendances->user_id = $row[0];
                        $attendances->created_by = Auth::user()->id;
                        $attendances->attendance_date =Carbon::parse(Date::excelToDateTimeObject ($row[2]));
                        $attendances->attendance_status  = $row[3];
                        $attendances->check_in = Carbon::parse(Date::excelToDateTimeObject ($row[4]));
                        $attendances->check_out = Carbon::parse(Date::excelToDateTimeObject ($row[5]));
                        $csv_check_in = Carbon::parse(Date::excelToDateTimeObject($row[4]));
                        $csv_check_out = Carbon::parse(Date::excelToDateTimeObject($row[5]));
                        if( $csv_check_in > $open){
                            $late_count = $csv_check_in->diff($open)->format('%H:%I:%S');
                            $attendances->late = $late_count;
                        }
                        if( $csv_check_out < $end){
                            $early_count = $csv_check_out->diff($end)->format('%H:%I:%S');
                            $attendances->early = $early_count;
                        }
    
                     $attendances->save();
                    }
    
                }
        }
    
    
        public function startRow(): int
        {
            return 2;
        }
    
    }
  • Related