Arato
Arato

Reputation: 25

Laravel 5.7: SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '35492' for column 'resident_dob' at row 1

I am trying to IMPORT an excel file containing some data and an error occurred

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '35492' for column 'resident_dob' at row 1

At first I was guessing that it might have been with the data in the excel file, and I've noticed that its format doesn't match the format in the SQL, So I've tried changing it but the same error have occurred. btw here's the Excel File Data

Here's the code for the Model:

public function model(array $row)
{
    return new Resident([
        'resident_fname' => $row[0],
        'resident_lname' => $row[1],
        'resident_mi'    => $row[2],
        'resident_dob'   => $row[3],
        'role'           => $row[4],
        'resident_age'   => $row[5],
        'resident_address'  => $row[6],
        'resident_contact'  => $row[7],
        'resident_email'    => $row[8],
        'resident_purok'    => $row[9],
        'resident_status'   => $row[10],
        'resident_gender'   => $row[11],
        'resident_religion' => $row[12],
        'ResidentVoter_status'  => $row[13],
    ]);
}

I really have no Idea with what to do with this Error. Can anyone help me with this? I'm still new to Laravel.

Upvotes: 1

Views: 6888

Answers (1)

Vinay
Vinay

Reputation: 7666

The issue is SQL doesn't support the date format you are feeding in the excel (unless chaged the default configuration)

By default SQL can store your date as 1997-03-03 00:00:00 which translates to Y-m-d H:i:s in PHP date format

So you need to manually convert it to the expected format either use DateTime or laravel's Carbon package. Below I have used Carbon

Declare this at the top

use Carbon;

And now convert the date

'resident_dob' => Carbon::parse($row[3])->format('Y-m-d H:i:s');

Upvotes: 5

Related Questions