Skip to content
Advertisement

Laravel – Avoiding double-booking time slots

I am trying to figure out how I would add some form of validation so that the same theatre slot cannot be booked twice on the same date. Any advice is much appreciated.

The code below is how to crete a booking, at present the user has a set list of options within the form to choose a theatre time slot in the TheatreRoomID field. The date is selected with the Form::Date input.

Bookings – Create.blade.php

JavaScript

BookingFormController:

JavaScript

BookingForm.php Model

JavaScript

BookingForms Table Structure: see table structure here

I am fairly new to Laravel so any suggestions are much appreciated.

Advertisement

Answer

Using Middleware:

  • First you define a Middleware using this command php artisan make:middleware MiddlewareName

I’ve already defind mine with the name CheckTheatre

So what Middleware does is filter the HTTP requests entering your application.

Like this:

JavaScript


so what this function does is check for the existence of a record with the same date and TheatreRoomID of the current request, (I assumed the date you’re referring to is the surgeryDate)

If there is a record it redirects him to home, But you can return an Error message if you like.

And if there wasn’t any record then the request passes and store is called.


Now after you made the function to handle the request you need to tell the Middleware which request to handle.
So inroutes.web You can call the middleware like this:

JavaScript

And make sure you add the Middleware you made to app/Http/Kernel.php

Suggestion

Read Mass Assignment & Form Request Validation it will make your controller much more readable and makes coding easier in general.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement