title: Laravel Eloquent / Carbon between two dates
body: <p>Some background first, I have a contact model to keep track of a user's contacts, and I have an anniversary model, they are linked as <code>Many-to-Many</code> with a pivot table (Each user can decide his/her own anniversaries and then add those to a contact, so the anniversaries are not hard coded).</p>
  <p>So each <code>User</code> hasMany <code>Anniversaries</code> and every <code>Contact</code> can have many <code>Anniversaries</code>, while an <code>Anniversary</code> can have many <code>Contacts</code>.</p>
  <p>For example, to get upcoming birthdays for all contacts for the next x months, I use the following query:</p>
  <pre><code>    auth()-&gt;user()-&gt;contacts()-&gt;whereBetween(DB::raw('DATE_FORMAT(birthdate, &quot;%m-%d&quot;)'), array(
          Carbon::now()-&gt;format('m-d'),
          Carbon::now()-&gt;addMonths($months)-&gt;format('m-d')))
          -&gt;get();
  </code></pre>
  <p>This works perfectly. However, for the anniversaries it is a bit more complicated. An anniversary at this moment can have a frequency of <code>monthly</code>, <code>quarterly</code>, <code>semi-annually</code> and <code>annually</code>. This is stored as a string in the pivot table <code>anniversary_contact</code>.</p>
  <p>I would like to use a similar query for this as the above one, but I am wondering if this is at all possible without having to repeat a lot of <code>orWhereBetween</code> like below:</p>
  <p>In the below query I check for upcoming anniversaries with a frequency of <code>monthly</code> for the next 3 days:</p>
  <pre><code>    $anniversaries = auth()-&gt;user()-&gt;contacts()-&gt;get()-&gt;each(function ($contact)
      {
          $contact-&gt;anniversaries-&gt;each(function ($anniversary) {
              return $anniversary-&gt;whereBetween(DB::raw('DATE_FORMAT(date, &quot;%m-%d&quot;)'), array(
                  Carbon::now()-&gt;format('m-d'),
                  Carbon::now()-&gt;addDays(3)-&gt;format('m-d')))
                  -&gt;orWhereBetween(DB::raw('DATE_FORMAT(date, &quot;%m-%d&quot;)'), array(
                      Carbon::now()-&gt;addMonths(1)-&gt;format('m-d'),
                      Carbon::now()-&gt;addDays(3)-&gt;format('m-d')))
                  -&gt;get();
  
                  // Left the rest of the 10 &quot;orWhereBetween&quot; out of it in this example
          });
      });
  </code></pre>
  <p>Although the above repetition is a seperate issue, my main issue is this:</p>
  <pre><code>DB::raw('DATE_FORMAT(date, &quot;%m-%d&quot;)')
  </code></pre>
  <p>There is no <code>date</code> field in the anniversaries table, it is in the pivot table.</p>
  <p>Main question: How to use the above query to get the results I need, from checking against the pivot date?</p>
  <p>Bonus question: Can the above query be made &quot;simpler&quot; or &quot;smaller&quot; without having to use a lot of <code>orWhereBetween</code> statements? If I need to check an upcoming anniversary that is a monthly one, I will have 12 <code>orWhereBetween</code> statements. Can this be reduced?</p>
  <p>Or should I forget about checking it on the database level and instead get all anniversaries and use Carbon to check against upcoming anniversaries?</p>