It will require two queries - so your implementation is okay. You could wrap the two queries in a transaction so the update/create are atomic.
updating one field in one table which create a new row in another
Hi folks, Pretty new to back end development in general and Laravel. I am hoping you guys can help me with this issue.
When i update a field in row in one table i would also like to create a row in another table in one swoop.
I have a patient appointment table with a field for a doctor_id, the idea is that the doctor can choose appointments. So when a doctor chooses the appointment i want to update that empty doctor_id field in the appointment table and subsequently create a new Chat Model so the doctor and patient can chat.
I am a little confused on what the best practice to do this? should it be a PUT HTTP Request or a POST HTTP?
here is what i have so far:
public function doctorTake($id){
$appointment = Appointment::find($id);
if(Auth::guard('doctor')->user()->id && !$appointment->doctor_id) {
$appointment->doctor_id = Auth::guard('doctor')->user()->id;
$appointment->save();
$chat = new Chat;
$chat->doctor_id = Auth::guard('doctor')->user()->id;
$chat->appointment_id = $appointment->id;
$chat->patient_id = $appointment->patient_id
$chat->save();
$response = ['appointment' => $appointment];
return response($response, 201);
}else{
return response([
'message' => 'Access denied, you do not have the permission.'
], 403);
}
}
Route::put('/doctor/open-appointments/{id}/take', [App\Http\Controllers\API\AppointmentController::class, 'doctorTake']);
Please or to participate in this conversation.