title: Filtering pivot table data with Laravel models
body: <p>Let's say I have three tables (this is just an example):</p>
  
  <pre><code>users
     user_id
     username
  
  roles
     role_id
     name
  
  user_roles
      user_id
      role_id
      primary (boolean)
  </code></pre>
  
  <p>And the corresponding laravel models:</p>
  
  <pre><code>   class User extends Eloquent {
          public function roles() {
                return $this-&gt;belongsToMany('Role')-&gt;withPivot('primary');
          }
     }
     class Role extends Eloquent {
           public function users() {
                return $this-&gt;belongsToMany('User')-&gt;withPivot('primary');
           }
     }
  </code></pre>
  
  <p>I want to get a list of all users, but with only the primary roles in the returned object.
  If I use something like:</p>
  
  <pre><code>$users = User::with('roles')-&gt;find(1);
  </code></pre>
  
  <p>Each user object will have a list of all the roles corresponding to it. I want this list to contain only the primary roles. Is there any way to do this from a query, without post processing the $users array?</p>