Laravel/Mysql: 无法将字段设置为空
Laravel/Mysql: unable to set field to null
我正在做一个 laravel 项目并试图在 mysql 数据库中复制一条记录。
复制后我想将字段值设置为 null(appointment_status)。
一切正常,除了新记录的值 (appointment_status) 与原始记录相同,即使我将其设置为 null。
$newAppointment = $appointment->replicate();
//push to get the id for the cloned record
$newAppointment->push();
$newAppointment->duplicated_from_id = $appointment->id;
$newAppointment->appointment_status = null;
$newAppointment->save();
//updating the old appointment
$appointment->duplicated_to_id = $newAppointment->id;
$appointment->save();
而不是整个代码块,尝试这样的事情:
// replicate as the new record with some different fields
$newAppointment = $appointment->replicate()->fill([
'duplicated_from_id' => $appointment->id,
'appointment_status' => null,
])->save();
// update some fields of initial original record
$appointment->update([
'duplicated_to_id' => $newAppointment->id,
]);
有关更多信息,请查看 this。
我正在做一个 laravel 项目并试图在 mysql 数据库中复制一条记录。 复制后我想将字段值设置为 null(appointment_status)。 一切正常,除了新记录的值 (appointment_status) 与原始记录相同,即使我将其设置为 null。
$newAppointment = $appointment->replicate();
//push to get the id for the cloned record
$newAppointment->push();
$newAppointment->duplicated_from_id = $appointment->id;
$newAppointment->appointment_status = null;
$newAppointment->save();
//updating the old appointment
$appointment->duplicated_to_id = $newAppointment->id;
$appointment->save();
而不是整个代码块,尝试这样的事情:
// replicate as the new record with some different fields
$newAppointment = $appointment->replicate()->fill([
'duplicated_from_id' => $appointment->id,
'appointment_status' => null,
])->save();
// update some fields of initial original record
$appointment->update([
'duplicated_to_id' => $newAppointment->id,
]);
有关更多信息,请查看 this。