title: Laravel 5.1 entering multiple duplicate records into the database
body: <p>I have written an application using Laravel 5.1.  The application is for parking spaces.  It "provisions" the spaces by creating 3 different available slots in the database (Morning, Afternoon, All Day).  </p>
  
  <p>The problem is that on a quick page refresh, the spaces are provisioned twice, resulting in 6 slots getting entered into the database.</p>
  
  <p>However, if I refresh the page after a 5 second delay, no further records are entered.  My if statement below seems to work, just not when the page / connection is refreshed quickly.</p>
  
  <pre><code>foreach($bays as $bay) {
      if(!BookingDates::where('date', $date)-&gt;where('parking_bay', $bay-&gt;number)-&gt;count()) {
          BookingDates::insert(['parking_bay' =&gt; $bay-&gt;number, 'date' =&gt; $date, 'slot' =&gt; 'Morning', 'time' =&gt; '7am - 1pm', 'status' =&gt; 'Available', 'created_at' =&gt; DB::raw('now()')]);
          BookingDates::insert(['parking_bay' =&gt; $bay-&gt;number, 'date' =&gt; $date, 'slot' =&gt; 'Afternoon', 'time' =&gt; '1pm - 7pm', 'status' =&gt; 'Available', 'created_at' =&gt; DB::raw('now()')]);
          BookingDates::insert(['parking_bay' =&gt; $bay-&gt;number, 'date' =&gt; $date, 'slot' =&gt; 'All Day', 'time' =&gt; null, 'status' =&gt; 'Available', 'created_at' =&gt; DB::raw('now()')]);
      }
  }
  </code></pre>
  
  <p>Does anyone know why this might happen?</p>