Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DIONRAP20's avatar

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']);
0 likes
5 replies
tykus's avatar

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.

1 like
DIONRAP20's avatar

@tykus If i were to wrap them in a DB::transaction, would the Route be a put or post? as there would still be the conflict of updating one table and creating the other.

Sinnbeck's avatar

@DIONRAP20 It does not matter in regards to transactions. They are a database related thing, not route related thing. So use put as suggested.

tykus's avatar

@DIONRAP20 the primary action is to take an existing Appointment; the creation of a Chat is a side-effect; so really PATCH is an appropriate HTTP method in this case; since you are making partial modifications to the Resource.

Don't get bent out of shape on this - depending on how you defined a Resource in your application, a POST request could also be argued for.

Sinnbeck's avatar

I would use PUT and use route model binding for resolving the Appointment.

1 like

Please or to participate in this conversation.