John Davidson

php - Laravel/Mysql: unable to set field to null

0 comments
Message:


I am working on a laravel project and trying to duplicate a record in mysql db.
after replication I want to set a field value to null(appointment_status).
everything is working except the new record's value (appointment_status) is the same as the original record even tho I set it to 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();


My Answer:
To set a field to null in Laravel, you can use the `setAttribute` method instead of directly setting the property. Here's how you can modify your code to set the `appointment_status` field to null:

php
$newAppointment = $appointment->replicate();
$newAppointment->push();
$newAppointment->duplicated_from_id = $appointment->id;
$newAppointment->setAttribute('appointment_status', null);
$newAppointment->save();

$appointment->duplicated_to_id = $newAppointment->id;
$appointment->save();


By using the `setAttribute` method, you can set the field to null and save the record with the updated value.

Rate this post

3 of 5 based on 1038 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us