title: How to get a list of posts and check if the user already "Liked" them? (Check existing relation between two models)
body: <p>I'm having fun with Laravel framework trying to build a little "blog" site that works the following way:</p>
  
  <ul>
  <li>When you enter the webpage, a cached index of the latest / most popular posts is shown</li>
  </ul>
  
  <p>There is a ManyToMany Relationship "user_likes" between the User and Post models, so that if you're logged in then you can "Like" or "upvote" each of these posts with a "heart" button, so that a relation between the two models will be created in the user_likes pivot table.</p>
  
  <p>I have two database tables</p>
  
  <p>users table:</p>
  
  <pre><code>user_id     name
     1        TheUser
  </code></pre>
  
  <p>posts table:</p>
  
  <pre><code>post_id     title
     1        MyPost
     2        Another Post
     3        Yet another post
  </code></pre>
  
  <p>user_likes pivot table:</p>
  
  <pre><code>post_id    user_id
    1           1
    3           1
  </code></pre>
  
  <p>And the User Model Class:</p>
  
  <pre><code>class User extends BaseUser {
  protected $table = 'users';
  
      public function likes()
      {
      return $this-&gt;belongsToMany('Post', 'user_likes','user_id','post_id');
      }
  
  }
  </code></pre>
  
  <ul>
  <li>The question is, what's an efficient way of retrieving a list of Post models and checking if the logged in User has already liked / upvoted them?</li>
  </ul>
  
  <p>The thing is that i'd like to check for every Post if the relation between the logged in User and the Post model exists in the most efficient way.</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>This SQL Fiddle would be a way of achieving what I'd like to do, the problem is that If i'm not wrong, I won't be able to cache the posts, as I'll need to check them every time a new user logs in.</p>
  
  <p><a href="http://www.sqlfiddle.com/#!2/e5bab/2" rel="nofollow">http://www.sqlfiddle.com/#!2/e5bab/2</a></p>
  
  <p>Using Laravel's Query builder:</p>
  
  <pre><code>DB::table('posts')
  -&gt;leftJoin('user_likes', function($join) {
      $join-&gt;on('user_likes.post_id', '=', 'posts.id')
          -&gt;where('user_likes.user_id', '=', 1);
  })
  
  -&gt;select('posts.id as post_id', 'posts.title', 'user_likes as did_i_like')
  -&gt;groupBy('posts.id')
  -&gt;orderBy('posts.id');
  </code></pre>
  
  <p>Right now, I'm doing it in my application in a different way:</p>
  
  <p>I have the paginated posts cached, and then each User has a cached collection of the posts ids that they liked, so when they log in, I'll check if each post is in the user's liked post collection:</p>
  
  <pre><code>$liked = Auth::user()-&gt;getLikedPosts(); // returns a cached collection of the User liked posts ids
  $posts = Post::popular(1); // returns a cached and paginated index of the most popular posts.
  </code></pre>
  
  <p>Then, I'll check if the liked collection contains the post id:</p>
  
  <pre><code>foreach($posts as $post)
  {
   .......
   if($liked-&gt;contains($post-&gt;id)
   {
   // show red Heart
   }
   else
   {
    // show gray heart
   }
   .......
  </code></pre>
  
  <p>I think It's not a good way of doing it, since the $liked collection will be growing a lot... However, with the new Query, I really don't know how could I cache the posts, so that I don't have to query the Database each time a user logs in or navigates throught the posts pages.</p>
  
  <p>Thanks in advance!</p>