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

@extends('layouts.app')
@section('content')
<hr>
<h1>Booking Form</h1>
<hr>
{!! Form::open(['action' => 'BookingFormsController@store', 'method' => 'POST']) !!}

<div class="form-group">
{{Form::label('requestID', 'Request ID')}}
{{Form::number('requestID', $patientDetail->requestID, ['class' => 'form-control',])}}
</div>

<div class="form-group">
{{Form::label('requestDate', 'Request Date')}}
{{Form::date('requestDate', $patientDetail->requestDate, ['class' => 'form-control'])}}
</div>

<div class="form-group">
{{Form::label('patientID', 'Patient ID')}}
{{Form::number('patientID', $patientDetail->patientID, ['class' => 'form-control'])}}
</div>

<div class="form-group">
{{Form::label('patientForename', 'Patient Forename')}}
{{Form::text('patientForename', $patientDetail->patientForename, ['class' => 'form-control'])}}
</div>

<div class="form-group">
{{Form::label('patientSurname', 'Patient Surname')}}
{{Form::text('patientSurname', $patientDetail->patientSurname, ['class' => 'form-control',])}}
</div>

<div class="form-group">
{{Form::label('patientSex', 'Patient Sex')}}
{{Form::text('patientSex', $patientDetail->patientSex, ['class' => 'form-control', 'placeholder' => ''])}}
</div>

<div class="form-group">
{{Form::label('patientDOB', 'Patient DOB')}}
{{Form::date('patientDOB', $patientDetail->patientDOB, ['class' => 'form-control','placeholder' => ''])}}
</div>

<div class="form-group">
{{Form::label('patientUrgency', 'Patient Urgency')}}
{{Form::text('patientUrgency', $patientDetail->patientUrgency, ['class' => 'form-control','placeholder' => ''])}}
</div>

<div class="form-group">
{{Form::label('TheatreRoomID', 'Theatre Room')}}
{{Form::select('TheatreRoomID', ['Room 1 - Time: 9:00AM - 11:00AM' => 'Room 1 - Time: 9:00AM - 11:00AM',
                                'Room 1 - Time: 12:00PM -2:00PM' => 'Room 1 - Time: 12:00PM - 2:00PM',
                                'Room 1 - Time: 3:00PM - 5:00PM' => 'Room 1 - Time: 3:00PM - 5:00PM',
                                'Room 2 - Time: 9:00AM - 11:00AM' => 'Room 2 - Time: 9:00AM - 11AM',
                                'Room 2 - Time: 12:00PM - 2:00PM' => 'Room 2 - Time: 12:00PM - 2:00PM',
                                'Room 2 - Time: 3:00PM - 5:00PM' => 'Room 2 - Time: 3:00PM - 5:00PM',
                                'Room 3 - Time: 9:00AM - 11:00AM' => 'Room 3 - Time: 9:00AM - 11:00AM',
                                'Room 3 - Time: 12:00PM - 2:00PM' => 'Room 3 - Time: 12:00PM - 2:00PM',
                                'Room 3 - Time: 3:00PM - 5:00PM' => 'Room 3 - Time: 3:00PM - 5:00PM'

                                ], null, ['class' => 'form-control','placeholder' => 'Select Theatre Slot'])}}
</div>

<div class="form-group">
{{Form::label('surgeryType', 'Surgery Type')}}
{{Form::text('surgeryType', $patientDetail->surgeryType, ['class' => 'form-control','placeholder' => ''])}}
</div>

<div class="form-group">
  {{Form::label('surgeryDate', 'Surgery Date')}}
  {{Form::date('surgeryDate')}}
</div>

<div class="form-group">
  {{Form::label('performingSurgeon', 'Peforming Surgeon')}}
  {{Form::select('performingSurgeon', ['Wendy clarke' => 'Wendy Clarke', 'John Kennedy' => 'John Kennedy', 'Imran Yousuf' => 'Imran Yousuf', 'Merideth Grey' => 'Merideth Grey', 'Derek Shepherd' => 'Derek Shepherd'], null, ['class' => 'form-control','placeholder' => ''])}}
</div>

<div class="form-group">
{{Form::label('bloodGroup', 'Blood Group')}}
{{Form::select('bloodGroup', ['A' => 'A', 'B' => 'B', 'O' => 'O', 'AB' => 'AB'], null, ['class' => 'form-control','placeholder' => $patientDetail->bloodGroup])}}
</div>

<div class="form-group">
  {{Form::label('patientNotes', 'Patient Notes')}}
  {{Form::textarea('patientNotes', '', ['class' => 'form-control', 'placeholder' => 'Enter any other neccessary patient details'])}}
</div>

<div class="btn-toolbar">
  <a href="javascript:history.back()" class="btn btn-danger mr-3">Back</a>
  {{Form::submit('Submit Booking', ['class'=> 'btn btn-success mr-3'])}}
</div>
{!! Form::close() !!}
@endsection

BookingFormController:

public function create()
{
    return view('bookingforms.create');
}

/**
 * Store a newly created resource in storage.
 *
 * @param  IlluminateHttpRequest  $request
 * @return IlluminateHttpResponse
 */
public function store(Request $booking)
{
  $this->validate($booking, [
  'requestID' => 'required',
  'patientID' => 'required',
  'patientForename' => 'required',
  'patientSurname'=> 'required',
  'patientSex' => 'required',
  'patientDOB' => 'required',
  'surgeryType' => 'required',
  'surgeryDate' => 'required',
  'performingSurgeon' => 'required',
  'TheatreRoomID' => 'required',
  'patientUrgency' => 'required',
  'patientNotes' => 'required',
  'bloodGroup' => 'required'
  ]);

  // Create new Booking Form
  $bookingform = new Bookingform;
  $bookingform->requestID = $booking->input('requestID');
  $bookingform->bookingID = $booking->input('bookingID');
  $bookingform->patientID = $booking->input('patientID');
  $bookingform->patientForename = $booking->input('patientForename');
  $bookingform->patientSurname = $booking->input('patientSurname');
  $bookingform->patientSex = $booking->input('patientSex');
  $bookingform->patientDOB = $booking->input('patientDOB');
  $bookingform->surgeryType = $booking->input('surgeryType');
  $bookingform->surgeryDate = $booking->input('surgeryDate');
  $bookingform->performingSurgeon = $booking->input('performingSurgeon');
  $bookingform->TheatreRoomID = $booking->input('TheatreRoomID');
  $bookingform->patientUrgency = $booking->input('patientUrgency');
  $bookingform->patientNotes = $booking->input('patientNotes');
  $bookingform->bloodGroup = $booking->input('bloodGroup');

  //Save Booking form

  $bookingform->save();

  //redirect
  return redirect('/bookingforms')->with('success', 'Booking Submitted');
}

BookingForm.php Model

    <?php

namespace App;

use IlluminateDatabaseEloquentModel;

class BookingForm extends Model
{
  protected $table = 'bookingforms';
  //primary key
  public $primaryKey = 'bookingID';
  //Timestamps
  public $timestamps = true;
}

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:

namespace AppHttpMiddleware;

use AppBookingForm;
use Closure;

class CheckTheatre
{
    /**
     * Handle an incoming request.
     *
     * @param IlluminateHttpRequest $request
     * @param Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
       return BookingForm::where([['surgeryDate',$request->input('surgeryDate')],
            ['TheatreRoomID',$request->input('TheatreRoomID')]])->exists() ? 
            redirect('home'):$next($request);
    }
}


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:

Route::post('bookingoffer', 'yourController@store')->middleware('MiddlewareName');

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