title: Laravel - Avoiding double-booking time slots
body: <p>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.</p>
  
  <p>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.</p>
  
  <p><strong>Bookings - Create.blade.php</strong></p>
  
  <pre><code>@extends('layouts.app')
  @section('content')
  &lt;hr&gt;
  &lt;h1&gt;Booking Form&lt;/h1&gt;
  &lt;hr&gt;
  {!! Form::open(['action' =&gt; 'BookingFormsController@store', 'method' =&gt; 'POST']) !!}
  
  &lt;div class="form-group"&gt;
  {{Form::label('requestID', 'Request ID')}}
  {{Form::number('requestID', $patientDetail-&gt;requestID, ['class' =&gt; 'form-control',])}}
  &lt;/div&gt;
  
  &lt;div class="form-group"&gt;
  {{Form::label('requestDate', 'Request Date')}}
  {{Form::date('requestDate', $patientDetail-&gt;requestDate, ['class' =&gt; 'form-control'])}}
  &lt;/div&gt;
  
  &lt;div class="form-group"&gt;
  {{Form::label('patientID', 'Patient ID')}}
  {{Form::number('patientID', $patientDetail-&gt;patientID, ['class' =&gt; 'form-control'])}}
  &lt;/div&gt;
  
  &lt;div class="form-group"&gt;
  {{Form::label('patientForename', 'Patient Forename')}}
  {{Form::text('patientForename', $patientDetail-&gt;patientForename, ['class' =&gt; 'form-control'])}}
  &lt;/div&gt;
  
  &lt;div class="form-group"&gt;
  {{Form::label('patientSurname', 'Patient Surname')}}
  {{Form::text('patientSurname', $patientDetail-&gt;patientSurname, ['class' =&gt; 'form-control',])}}
  &lt;/div&gt;
  
  &lt;div class="form-group"&gt;
  {{Form::label('patientSex', 'Patient Sex')}}
  {{Form::text('patientSex', $patientDetail-&gt;patientSex, ['class' =&gt; 'form-control', 'placeholder' =&gt; ''])}}
  &lt;/div&gt;
  
  &lt;div class="form-group"&gt;
  {{Form::label('patientDOB', 'Patient DOB')}}
  {{Form::date('patientDOB', $patientDetail-&gt;patientDOB, ['class' =&gt; 'form-control','placeholder' =&gt; ''])}}
  &lt;/div&gt;
  
  &lt;div class="form-group"&gt;
  {{Form::label('patientUrgency', 'Patient Urgency')}}
  {{Form::text('patientUrgency', $patientDetail-&gt;patientUrgency, ['class' =&gt; 'form-control','placeholder' =&gt; ''])}}
  &lt;/div&gt;
  
  &lt;div class="form-group"&gt;
  {{Form::label('TheatreRoomID', 'Theatre Room')}}
  {{Form::select('TheatreRoomID', ['Room 1 - Time: 9:00AM - 11:00AM' =&gt; 'Room 1 - Time: 9:00AM - 11:00AM',
                                  'Room 1 - Time: 12:00PM -2:00PM' =&gt; 'Room 1 - Time: 12:00PM - 2:00PM',
                                  'Room 1 - Time: 3:00PM - 5:00PM' =&gt; 'Room 1 - Time: 3:00PM - 5:00PM',
                                  'Room 2 - Time: 9:00AM - 11:00AM' =&gt; 'Room 2 - Time: 9:00AM - 11AM',
                                  'Room 2 - Time: 12:00PM - 2:00PM' =&gt; 'Room 2 - Time: 12:00PM - 2:00PM',
                                  'Room 2 - Time: 3:00PM - 5:00PM' =&gt; 'Room 2 - Time: 3:00PM - 5:00PM',
                                  'Room 3 - Time: 9:00AM - 11:00AM' =&gt; 'Room 3 - Time: 9:00AM - 11:00AM',
                                  'Room 3 - Time: 12:00PM - 2:00PM' =&gt; 'Room 3 - Time: 12:00PM - 2:00PM',
                                  'Room 3 - Time: 3:00PM - 5:00PM' =&gt; 'Room 3 - Time: 3:00PM - 5:00PM'
  
                                  ], null, ['class' =&gt; 'form-control','placeholder' =&gt; 'Select Theatre Slot'])}}
  &lt;/div&gt;
  
  &lt;div class="form-group"&gt;
  {{Form::label('surgeryType', 'Surgery Type')}}
  {{Form::text('surgeryType', $patientDetail-&gt;surgeryType, ['class' =&gt; 'form-control','placeholder' =&gt; ''])}}
  &lt;/div&gt;
  
  &lt;div class="form-group"&gt;
    {{Form::label('surgeryDate', 'Surgery Date')}}
    {{Form::date('surgeryDate')}}
  &lt;/div&gt;
  
  &lt;div class="form-group"&gt;
    {{Form::label('performingSurgeon', 'Peforming Surgeon')}}
    {{Form::select('performingSurgeon', ['Wendy clarke' =&gt; 'Wendy Clarke', 'John Kennedy' =&gt; 'John Kennedy', 'Imran Yousuf' =&gt; 'Imran Yousuf', 'Merideth Grey' =&gt; 'Merideth Grey', 'Derek Shepherd' =&gt; 'Derek Shepherd'], null, ['class' =&gt; 'form-control','placeholder' =&gt; ''])}}
  &lt;/div&gt;
  
  &lt;div class="form-group"&gt;
  {{Form::label('bloodGroup', 'Blood Group')}}
  {{Form::select('bloodGroup', ['A' =&gt; 'A', 'B' =&gt; 'B', 'O' =&gt; 'O', 'AB' =&gt; 'AB'], null, ['class' =&gt; 'form-control','placeholder' =&gt; $patientDetail-&gt;bloodGroup])}}
  &lt;/div&gt;
  
  &lt;div class="form-group"&gt;
    {{Form::label('patientNotes', 'Patient Notes')}}
    {{Form::textarea('patientNotes', '', ['class' =&gt; 'form-control', 'placeholder' =&gt; 'Enter any other neccessary patient details'])}}
  &lt;/div&gt;
  
  &lt;div class="btn-toolbar"&gt;
    &lt;a href="javascript:history.back()" class="btn btn-danger mr-3"&gt;Back&lt;/a&gt;
    {{Form::submit('Submit Booking', ['class'=&gt; 'btn btn-success mr-3'])}}
  &lt;/div&gt;
  {!! Form::close() !!}
  @endsection
  </code></pre>
  
  <p><strong>BookingFormController:</strong></p>
  
  <pre><code>public function create()
  {
      return view('bookingforms.create');
  }
  
  /**
   * Store a newly created resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @return \Illuminate\Http\Response
   */
  public function store(Request $booking)
  {
    $this-&gt;validate($booking, [
    'requestID' =&gt; 'required',
    'patientID' =&gt; 'required',
    'patientForename' =&gt; 'required',
    'patientSurname'=&gt; 'required',
    'patientSex' =&gt; 'required',
    'patientDOB' =&gt; 'required',
    'surgeryType' =&gt; 'required',
    'surgeryDate' =&gt; 'required',
    'performingSurgeon' =&gt; 'required',
    'TheatreRoomID' =&gt; 'required',
    'patientUrgency' =&gt; 'required',
    'patientNotes' =&gt; 'required',
    'bloodGroup' =&gt; 'required'
    ]);
  
    // Create new Booking Form
    $bookingform = new Bookingform;
    $bookingform-&gt;requestID = $booking-&gt;input('requestID');
    $bookingform-&gt;bookingID = $booking-&gt;input('bookingID');
    $bookingform-&gt;patientID = $booking-&gt;input('patientID');
    $bookingform-&gt;patientForename = $booking-&gt;input('patientForename');
    $bookingform-&gt;patientSurname = $booking-&gt;input('patientSurname');
    $bookingform-&gt;patientSex = $booking-&gt;input('patientSex');
    $bookingform-&gt;patientDOB = $booking-&gt;input('patientDOB');
    $bookingform-&gt;surgeryType = $booking-&gt;input('surgeryType');
    $bookingform-&gt;surgeryDate = $booking-&gt;input('surgeryDate');
    $bookingform-&gt;performingSurgeon = $booking-&gt;input('performingSurgeon');
    $bookingform-&gt;TheatreRoomID = $booking-&gt;input('TheatreRoomID');
    $bookingform-&gt;patientUrgency = $booking-&gt;input('patientUrgency');
    $bookingform-&gt;patientNotes = $booking-&gt;input('patientNotes');
    $bookingform-&gt;bloodGroup = $booking-&gt;input('bloodGroup');
  
    //Save Booking form
  
    $bookingform-&gt;save();
  
    //redirect
    return redirect('/bookingforms')-&gt;with('success', 'Booking Submitted');
  }
  </code></pre>
  
  <p><strong>BookingForm.php Model</strong></p>
  
  <pre><code>    &lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class BookingForm extends Model
  {
    protected $table = 'bookingforms';
    //primary key
    public $primaryKey = 'bookingID';
    //Timestamps
    public $timestamps = true;
  }
  </code></pre>
  
  <p><strong>BookingForms Table Structure:</strong>
  <a href="https://i.stack.imgur.com/tr6ev.png" rel="nofollow noreferrer">see table structure here</a></p>
  
  <p>I am fairly new to Laravel so any suggestions are much appreciated.</p>