noblemfd's avatar

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'null' for column `myapp`.`employees`.`state_id`

In a project I am using Angular-12 as frontend and Laravel-8 as backend.

Laravel:

        'state_id' => [
            'nullable'
        ],
        'city_id' => [
            'nullable'
        ],

    $employee = Employee::find($id);
    $employee->nationality_id = $request->nationality_id;
    $employee->state_id = $request->state_id ?? null;
    $employee->city_id = $request->city_id ?? null;
    $employee->save();

It is a dependent dropdown. Whenever state_id is not selected I got this error:

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'null' for column myapp.employees.state_id

Same thing with city_id

Note: state_id and city_id are nullable in the DB.

state_id, city_id are integers. But if there is value in it, it is inserted without any error.

How do I get this resolved?

Thanks

0 likes
5 replies
Nakov's avatar

If the column in the database is nullable then use null without the quotes

$employee->state_id = $request->state_id ?? null;
jlrdw's avatar

@nakov just curious, where does he have those quotes, in my browser Firefox, his question doesn't have quotes, he has:

$employee->state_id = $request->state_id ?? null;

Please or to participate in this conversation.