title: Laravel Eloquent date range between range
body: <p>I am building a website where i have a booking mechanism.
  The user can book a hotel room for X days if the room is available.</p>
  
  <p>I am using:</p>
  
  <ul>
  <li>Laravel 5.4</li>
  <li>MySql</li>
  </ul>
  
  <p>The room is unavailable if:</p>
  
  <ul>
  <li>It is already booked by another user</li>
  <li>The admin has set it as unavailable</li>
  <li>The room capacity is less or equal to the number of travellers</li>
  </ul>
  
  <p>If have 3 tables to store those data:</p>
  
  <ul>
  <li>Rent: Contains the booking infos, such as rentStartDate and rentEndDate (as DateTime) and other fields (userId, rentalId, ...)</li>
  <li>Unavailabilities: When the admin set a room as unavailable, it's stored here. I have the fields unavailabilityStartDate, unavailabilityEndDate (as DateTime) and rentalId</li>
  <li>Rentals: This table contain all the infos regarding the room (capacity ,name, location, ...)</li>
  </ul>
  
  <p>I am struggling to build a Eloquent query to check if the room is available before processing the user payment. Here is what i have for now:</p>
  
  <pre><code>public function isAvailableAtDates($rentalId, $startDate, $endDate, $capacity) {
      $from = min($startDate, $endDate);
      $till = max($startDate, $endDate);
  
      $result = DB::table('rentals')
          -&gt;where([
              ['rentals.rentalId', '=', $rentalId],
              ['rentals.rentalCapacity', '&gt;=', $capacity]
          ])
          -&gt;whereNotIn('rentals.rentalId', function($query) use ($from, $till, $rentalId) {
              $query-&gt;select('unavailabilities.rentalId')
                  -&gt;from('unavailabilities')
                  -&gt;where([
                      ['unavailabilities.rentalId', '=', $rentalId],
                      ['unavailabilityStartDate', '&gt;=', $from],
                      ['unavailabilityEndDate', '&lt;=', $till],
                  ]);
  
          })
          -&gt;whereNotIn('rentals.rentalId', function($query) use ($from, $till, $rentalId) {
              $query-&gt;select('rent.rentalId')
                  -&gt;from('rent')
                  -&gt;where([
                      ['rent.rentalId', '=', $rentalId],
                      ['rentStartDate', '&gt;=', $from],
                      ['rentEndDate', '&lt;=', $till]
                  ]);
          })
          -&gt;select('rentals.rentalId')
          -&gt;get();
  
      return count($result) == 1;
  }
  </code></pre>
  
  <p>Let's say I have a row inside Unavailabilities with:</p>
  
  <ul>
  <li>unavailabilityStartDate = 2017-04-26 00:00:00</li>
  <li>unavailabilityEndDate = 2017-04-30 00:00:00</li>
  </ul>
  
  <p>When calling the method with some dates outside of the range stored in Unavailabilities, i'm getting the expected result. When calling it with the exact same dates, i'm getting no result (which is what i want).
  So far so good!
  The problem is if i'm calling it with a start date between 26 of April and 30th and a end date later in May, i am still getting a result even tho i shouldn't.</p>
  
  <p>Could anyone help me with that?</p>