title: Laravel with MySQL and Redis, best approach to use - filtering items with hasMany()
body: <p>I'm working on an app and would like to use the Redis for the first time to make part of the queries faster. Just to be sure, I would like to keep my MySQL in the background as a fallback and to keep data related in a way it's familiar to me.</p>
  
  <p>Let me describe the scenario I have:</p>
  
  <p>I have three models: <code>category</code>, <code>subcategory</code> and <code>events</code>.</p>
  
  <p><code>events</code> columns are: <code>id</code>,<code>subcategory_id</code>,<code>occurs_at</code>,<code>parameter_</code>,<code>parameter_2</code>, ...</p>
  
  <p><code>events</code> belong to the <code>subcategory</code> and <code>subcategory</code> belongs to the <code>category</code>. These are one to many relations.</p>
  
  <p>My idea was following:</p>
  
  <p>Whenever the <code>events</code> instance is created I create the Redis <a href="http://redis.io/topics/data-types-intro" rel="nofollow noreferrer">hash entry</a> (forgive me my syntax as it was some time that I studied Redis):</p>
  
  <p><code>event:{event_id} category 4 subcategory 25 at "occurs_at" parameter_1 12 paremeter_2 55</code></p>
  
  <p>Now that I have these in Redis I would like to do the following queries:</p>
  
  <ul>
  <li>go through all events and get unique category_id's -> go back to MySQL and fetch the information of these categories (as description and such) using Eloquent</li>
  <li>go through all events and get filtered (using <a href="http://redis.io/commands/scan" rel="nofollow noreferrer">MATCH</a>) category_id's, for example where parameter_1 larger than 245 -> and again go back to MySQL and fetch the information of these categories (as description and such) using Eloquent</li>
  </ul>
  
  <p>First question is: is this a good approach? I would like to have all the data in my MySQL db just in case but still utilize the Redis to familiarize myself with it and make the queries faster.</p>
  
  <p>Second idea: Should I use Eloquent to fetch the <code>category</code> instance after I got the proper ones from the Redis or should I save all of the data in the Redis hash as:</p>
  
  <p><code>event:{event_id} category 4 subcategory 25 at "occurs_at" parameter_1 12 paremeter_2 55 category_parameter_1 "some data from category model" category_parameter_2 "some other data from category model"</code></p>
  
  <p>Third idea: make another set of hashes for categor and fetch them instead of the data from the eloquent.</p>
  
  <p><code>category:{category_id} category_parameter_1 "some data" category_parameter_2 "more data"</code></p>
  
  <p>Any thoughts, pointers, links to similar tutorial are more than welcome. Thanks!</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Example Eloquent query:</p>
  
  <p>In <code>category</code> model:</p>
  
  <pre><code>public function events()
  {
    return $this-&gt;hasManyThrough('Event', 'Subcategory');
  }
  </code></pre>
  
  <p>Then a query would be:</p>
  
  <pre><code>$categories = Category::whereHas('events', function ($query) {
      $query-&gt;where('occurs_at', '&gt;=', Carbon::today());
  })-&gt;get();
  </code></pre>
  
  <p>Hope I didn't miss something..</p>