laravel/framework — pull request #5752 by JamShady (2014-09-16 16:15:24+00:00)
Title: Pivot table correctly respects wherePivot() conditions

Consider a pivot table being used to match pets and their owners, e.g.
````
owner_id
pet_type
pet_id
````
The classes would be:
````
abstract class Pet extends BaseModel {
public function owner() {
return $this->belongsToMany('Owner', 'owner_pets', 'pet_id')
->wherePivot('pet_type', '=', get_class($this));
}
}
class Cat extends Pet {}
class Dog extends Pet {}
````
This _almost_ works, the problem being that when `BelongsToMany::sync()` is called, it doesn't respect the `wherePivot()` restrictions, causing updates to apply across all pets, and not just the specific ones it should be restricted to.

This update ensures that any sub-queries also respect the same conditions/restrictions.

I also changed `BelongsToMany::newPivotStatementForId($id)` to call `BelongsToMany::newPivotQuery()` because they were almost identical, and this would also allow it to observe the restrictions neatly