In this episode, we get hands-on and actually build out the part of our app that stores new appointments!
Here's how it goes:
- We start by setting up a new POST route and an AppointmentController to handle appointment submissions from our frontend checkout form.
- We go step-by-step wiring up the form to submit with Alpine.js and Axios, making sure all the relevant data (like employee ID, service ID, date, time, customer name, and email) gets sent correctly to the backend.
- After testing the data flow, we create a Laravel Form Request class to handle validation. This ensures things like dates are real dates, emails are valid, and the provided employee/service IDs actually exist in the database.
- The tricky part is making sure the appointment knows when it starts and when it ends. To figure out the end time, we use the selected service’s duration and add that onto the chosen date and time, using Carbon for easy date manipulation.
- We try a couple of different approaches for where to do this calculation—first inside the Form Request, then realize it makes more sense to move it to the controller after validation. This way, our code stays clean and maintainable.
- Finally, we insert the appointment into the database. We double-check everything works as expected—appointments are saved, unavailable slots disappear from the booking form, and everything's hooked up nicely.
By the end, we've got a fully working appointment booking flow, including all the validation and time calculations. Next up, we'll handle more advanced stuff, like avoiding overlapping appointments!