title: Laravel where clause on object?
body: <p>I have 4 million rows in my database which I need to filter with time, as you all can guess the problem is about speed.</p>
  
  <p>I am currently using the <code>remember</code> function to cache my queries. But I have some exact queries which my user are allowed so select. What i mean by this is:</p>
  
  <ul>
  <li>All data of the last year </li>
  <li>All data of the 6 months</li>
  <li>All data of the last month</li>
  <li>All data of the last week</li>
  </ul>
  
  <p>Which means I have 4 queries to cache... All of them take about 30 seconds to be taken if you run the query without cache.</p>
  
  <p>The thing I want is do is to filter that data after I receive all data <em>once</em> and cache it.</p>
  
  <p>So that I can use the <strong>all data</strong> to filter how I want.</p>
  
  <p>A bit more clearly with some code:</p>
  
  <pre><code>&lt;?php
  
  if (Cache::has('alldata')) {
      $alldata = Cache::get('alldata');
  } else {
      $alldata = Data::all();
  }
  
  // Filters
  $alldata-&gt;where('activity', '=', 1)-&gt;get();
  
  // The code i currently use for every filter
  $lastYearData = Data::where('column', '=', 'filter')-&gt;get();
  // The line above "4 times" for each filter
  
  ?&gt;
  </code></pre>