title: Laravel - Cache results of relationship tables using Eloquent queries
body: <p>I have a simple relationship Post x Files in my app (one post has many files and one file is associated to only one post). To make things more agile, I store the posts on the cache whenever it is not changed so I don't need to query the database again. The problem that I noticed is that only the Posts are being stored in the cache and not the files (and I suppose the problem is because the way I query them). My code is:</p>
  
  <pre><code>class Post extends Model
  {
  
  public function files(){
      return $this-&gt;hasMany('Ibbr\File');
  }
  
  }
  </code></pre>
  
  <p>The function for obtaining the posts:</p>
  
  <pre><code>public static function pegaPostsBoard($nomeBoard)
  {
      $chave = 'posts_board';
      if(Cache::has($chave))
          return Cache::get($chave);
  
      $posts = Post::orderBy('updated_at', 'desc')-&gt;where('board', $nomeBoard)-&gt;where('lead_id', null)-&gt;paginate(10);
      Cache::forever($chave, $posts); //I "forget" the cache whenever the post is changed
      return $posts;
  }
  </code></pre>
  
  <p>I also tried adding <code>-&gt;join('files', 'posts.id', '=', 'files.post_id')</code> before adding it to the cache but it didn't work. How did I noticed that the files are not being cached? Well, I reset the database so it cleans all the rows and I noticed that If I F5 the page the posts are still there (because they are cached) but not their files. So my question is how do I query it in such a way that the files are also stored?</p>