title: How to query only one foreign row in a one-to-many relationship in Laravel 5
body: <p>I have 4 models: <code>posts</code>, <code>users</code>, <code>categories</code>, <code>userCategories</code>.</p>
  
  <p><code>posts</code>-<code>users</code> have one-to-many relationship (one post belongs to a user).</p>
  
  <p><code>posts</code>-<code>categories</code> have one-to-many relationship (one post belongs to a category).</p>
  
  <p><code>users</code>-<code>userCategories</code> have one-to-many relationship (one user has many userCategories). userCategory has a <code>rank</code> field which holds the performance of the user for the specific category.</p>
  
  <p>I query some posts with this:</p>
  
  <pre><code>$posts = Post::with(array('user.userCategories')
                  )
              -&gt;where('category_id', '=', $category_id)
              -&gt;get();
  </code></pre>
  
  <p>After evaluating posts, I try to get the user's the userCategory <code>rank</code> for the post's category.</p>
  
  <pre><code>$rank_of_the_post_owners = array();
  foreach($posts as $post)
      foreach($post-&gt;user-&gt;userCatergories as $userCategory)
          if($userCategory-&gt;id == $post-&gt;category_id) {
              $rank_of_the_post_owners [$post-&gt;id] = $userCategory-&gt;rank;
              break;
          }
      }
  }
  </code></pre>
  
  <p>To get rid of the code above, I want to query only related userCategory (for the post's category). So that, I want to access the rank information like user.userCategory->rank instead of the loop.</p>
  
  <p>How can I do that?</p>