【问题标题】:Laravel/Mysql: unable to set field to nullLaravel/Mysql:无法将字段设置为空
【发布时间】:2021-06-20 08:20:50
【问题描述】:

我正在开发一个 laravel 项目并试图在 mysql db 中复制一条记录。 复制后,我想将字段值设置为 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();

【问题讨论】:

    标签: php mysql laravel voyager


    【解决方案1】:

    而不是整个代码块,尝试这样的事情:

    // 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

    【讨论】:

      猜你喜欢
      • 2020-02-18
      • 2020-09-20
      • 2019-11-08
      • 2015-07-10
      • 2016-01-23
      • 2014-09-01
      • 2011-06-08
      • 2016-09-29
      • 2014-08-07
      相关资源
      最近更新 更多