title: Laravel 5 issue with wherePivot
body: <p>I am working with Laravel 5 and I am having issue getting <code>-&gt;wherePivot()</code> to work on a Many-to-Many relationship. When I <code>dd()</code> the SQL it looks like Eloquent is looking for records in the pivot table with a `pose_state`.`pose_id` is null`. </p>
  
  <p>I am hoping it is a simple error and not a bug. Any ideas are appreciated.</p>
  
  <h3>Database Structure</h3>
  
  <p><strong>pose</strong></p>
  
  <pre><code>id
  name
  type
  </code></pre>
  
  <p><strong>state</strong></p>
  
  <pre><code>id
  name
  machine_name
  </code></pre>
  
  <p><strong>pose_state</strong></p>
  
  <pre><code>pose_id
  state_id
  status
  </code></pre>
  
  <h3>Models</h3>
  
  <p><strong>Pose</strong></p>
  
  <pre><code>&lt;?php namespace App;
  use DB;
  use App\State;
  use Illuminate\Database\Eloquent\Model;
  
  class Pose extends Model {
  
    public function states()
    {
        return $this-&gt;belongsToMany('App\State')
                    -&gt;withPivot('status_id')
                    -&gt;withTimestamps();
    }
    public function scopeWithPendingReviews()
    {
        return $this-&gt;states()
                    -&gt;wherePivot('status_id',10);
    }
  }
  </code></pre>
  
  <p><strong>State</strong></p>
  
  <pre><code>&lt;?php namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class State extends Model {
  
      public function poses()
      {
        return $this-&gt;belongsToMany('Pose')
                    -&gt;withPivot('status_id')
                    -&gt;withTimestamps();
      }
  
  }
  </code></pre>
  
  <p><strong>PosesController function</strong></p>
  
  <pre><code>public function listPosesForReview(){
      $poses = Pose::withPendingReviews()-&gt;get();
      dd($poses-&gt;toArray() );
  }
  </code></pre>
  
  <p><strong>SQL</strong></p>
  
  <pre><code>select 
    `states`.*, `pose_state`.`pose_id` as `pivot_pose_id`,
    `pose_state`.`state_id` as `pivot_state_id`, 
    `pose_state`.`status_id` as `pivot_status_id`, 
    `pose_state`.`created_at` as `pivot_created_at`, 
    `pose_state`.`updated_at` as `pivot_updated_at` 
  from 
    `states` inner join `pose_state` on `states`.`id` = `pose_state`.`state_id` 
  where 
    `pose_state`.`pose_id` is null and `pose_state`.`status_id` = ?
  </code></pre>
  
  <h1>EDIT</h1>
  
  <p>When I updated my code to removing the scope it worked. Thanks @Deefour for putting me on the right path! Maybe scope has something else to that I am missing.</p>
  
  <pre><code>public function pendingReviews()
  {
    return $this-&gt;states()
                -&gt;wherePivot('status_id','=', 10);
  }
  </code></pre>
  
  <h1>YET ANOTHER EDIT</h1>
  
  <p>I finally got this to work. The solution above was giving me duplicate entries. No idea why this works, but it does, so I will stick with it.</p>
  
  <pre><code>public function scopeWithStatusCode($query, $tag)
  {
    $query-&gt;with(['states' =&gt; function($q) use ($tag)
              {
                $q-&gt;wherePivot('status_id','=', $tag);
              }])
          -&gt;whereHas('states',function($q) use ($tag)
              {
                $q-&gt;where('status_id', $tag);
              });
  }
  </code></pre>