In this episode, we focus on actually creating the appointment record in our application. We refactor the code a bit to make things cleaner, moving the appointment creation logic to its own method. This will help when we want to redirect after the appointment gets created.
We start by grabbing the form inputs (like name, email, and the selected date/time), and then use the Carbon library to combine the selected date and time into a proper DateTime object for when the appointment starts. We calculate the end time by adding the relevant service duration to the start time, making sure to copy the start DateTime object so we don't accidentally modify it when working out the end time.
Once that's set up, we go ahead and link (associate) the appointment with the chosen employee and service, which are both set up as relationships in our models. We do a couple of test bookings just to verify that the data—name, email, start time, end time, employee, and service—are all correct and associated properly. We finish off by actually saving the appointment to the database and confirming that it appears as expected. Plus, the slot we just booked now disappears from the available slots, so everything is wired up nicely!