Reputation: 3424
I'm using Laravel 4.2 Query Scopes but encountered a problem.
My Model:
class SomeModel extends Eloquent {
public function scopeS1($query) {
return $query->where('field1', '=', 'S1');
}
public function scopeS2($query) {
return $query->where('field2', '=', 'S2');
}
}
Now when I do SomeModel::s1()->s2()->get();
it returns all results and doesn't filter by S1
AND S2
. Note also that I have no problem when I do
SomeModel::where('field1', '=', 'S1')->where('field2', '=', 'S2')->get()
So why is query scoping and doing anything here??
Upvotes: 0
Views: 271
Reputation: 152860
Since your real scopes contain OR conditions you should use a nested where to make sure they get interpreted correctly. Laravel will wrap parentheses around.
public function scopeS1($query) {
return $query->where(function($q){
$q->where('field1', '=', 'S1')
->orWhere('foo', '=', 'bar');
});
}
// and the same for scopeS2...
Upvotes: 3