title: How to get multiple whereBetween records in Laravel
body: <p>I'm building a small application on <code>Laravel 5.4</code> where I'm having data sets to are being fetched from the relationship something like this:</p>
  
  <pre><code>$meetings = Company::where('name', 'Atlanta Ltd')
      -&gt;withCount(['interactions as investors'=&gt; function ($q) {
          $q
              -&gt;whereBetween('schedule', [Carbon::now()-&gt;subMonths(6), Carbon::now()-&gt;subMonths(1)])
              -&gt;whereHas('contactsAssociation', function ($q) {
                  $q-&gt;whereHas('company', function ($q) {
                      $q-&gt;where('type', 'like', 'Investor');
                  });
              });
      }])-&gt;get()
      -&gt;transform(function ($company) {
          $company-&gt;investors_count = $company-&gt;interactions
              -&gt;pluck('investors_count')
              -&gt;sum();
          return $company;
      });
  </code></pre>
  
  <p>Currently I'm getting the number of interaction as investor_counts between that particular date, I want to fetch the counts between certain date fields, i.e. suppose I want to have the data to be fetched for consecutive 6 months, I mean data of 6 different months something like this:</p>
  
  <pre><code>-&gt;whereBetween('schedule', [Carbon::now()-&gt;subMonths(6), Carbon::now()-&gt;subMonths(5)])
  </code></pre>
  
  <p>and</p>
  
  <pre><code>-&gt;whereBetween('schedule', [Carbon::now()-&gt;subMonths(5), Carbon::now()-&gt;subMonths(4)])
  </code></pre>
  
  <p>and</p>
  
  <pre><code>-&gt;whereBetween('schedule', [Carbon::now()-&gt;subMonths(4), Carbon::now()-&gt;subMonths(3)])
  </code></pre>
  
  <p>And the list goes on similar to above differences. And I don't want to implement multiple foreach loops, is there any way out with <code>Laravel Collection</code> where I can manipulate such data the get the output. Please guide me. Thanks.</p>
  
  <p><strong>Edit:</strong>
  Let me explain in better way, currently I'm using this code to get my desired data:</p>
  
  <pre><code>public function investorGraphData(Request $request)
  {
      $weekInvestorData = $weekResearchData = [];
  
      if($request-&gt;timeFormat == 'month')
      {
          for($i=0; $i&lt;6; $i++)
          {
              $meetings = Company::where('name', $request-&gt;client)
                  -&gt;withCount(['interactions as investors'=&gt; function ($q) use($i) {
                      $q
                          -&gt;whereBetween('schedule', [Carbon::now()-&gt;subMonth($i+1), Carbon::now()-&gt;subMonth($i)])
                          -&gt;whereHas('contactsAssociation', function ($q) {
                              $q-&gt;whereHas('company', function ($q) {
                                  $q-&gt;where('type', 'like', 'Investor');
                              });
                          });
                  }])
                  -&gt;withCount(['interactions as research' =&gt; function($q) use($i) {
                      $q
                          -&gt;whereBetween('schedule', [Carbon::now()-&gt;subMonth($i+1), Carbon::now()-&gt;subMonth($i)])
                          -&gt;whereHas('contactsAssociation', function ($q) {
                              $q-&gt;whereHas('company', function ($q) {
                                  $q-&gt;where('type', 'like', 'Research');
                              });
                          });
                  }])
                  -&gt;get();
              $weekInvestorData[] = $meetings[0]-&gt;investors_count;
              $weekResearchData[] = $meetings[0]-&gt;research_count;
          }
  
          return response()-&gt;json(['investor' =&gt; $weekInvestorData, 'research' =&gt; $weekResearchData], 200);
      }
      elseif ($request-&gt;timeFormat == 'week')
      {
          for($i=0; $i&lt;6; $i++)
          {
              $meetings = Company::where('name', $request-&gt;client)
                  -&gt;withCount(['interactions as investors'=&gt; function ($q) use($i) {
                      $q
                          -&gt;whereBetween('schedule', [Carbon::now()-&gt;subWeek($i+1), Carbon::now()-&gt;subWeek($i)])
                          -&gt;whereHas('contactsAssociation', function ($q) {
                              $q-&gt;whereHas('company', function ($q) {
                                  $q-&gt;where('type', 'like', 'Investor');
                              });
                          });
                  }])
                  -&gt;withCount(['interactions as research' =&gt; function($q) use($i) {
                      $q
                          -&gt;whereBetween('schedule', [Carbon::now()-&gt;subWeek($i+1), Carbon::now()-&gt;subWeek($i)])
                          -&gt;whereHas('contactsAssociation', function ($q) {
                              $q-&gt;whereHas('company', function ($q) {
                                  $q-&gt;where('type', 'like', 'Research');
                              });
                          });
                  }])
                  -&gt;get();
              $weekInvestorData[] = $meetings[0]-&gt;investors_count;
              $weekResearchData[] = $meetings[0]-&gt;research_count;
          }
          return response()-&gt;json(['investor' =&gt; $weekInvestorData, 'research' =&gt; $weekResearchData], 200);
      }
      else
          return response()-&gt;json(['message' =&gt; 'No input given'], 200);
  }
  </code></pre>
  
  <p>Is there any way to shorten this code through collection?</p>