Reputation: 193
This Problem blows my mind at the moment:
I have the following relation:
Groups --> Intermediate Many-To-Many Table <-- Cities
(Every Group can have multiple Cities assigned)
Now I want to get all the Group-Models where the assigned City is either id X and id Y. Like Saying "get me all the groups which are assigned to Boston and New York"
Is that possible with Laravel 3's Eloquent?
Thank you very much! Matthias
Upvotes: 1
Views: 263
Reputation: 846
In my experience, where clauses do not work inside of many-to-many relationships. But they do work if you eager load them.
Group::with(array('cities' => function($q) {
$q->or_where('id', '=', X);
$q->or_where('id', '=', Y);
})->get();
Upvotes: 2
Reputation: 3949
Extending aowie1's answer above (which is correct), it's often useful to nest or_where clauses (as this makes logical sense, especially if you're looking to expand the query with non-or-where conditions):
Group::with(array('cities' => function($q) {
$q->where(function($where) {
$where->or_where('id', '=', 'x');
$where->or_where('id', '=', 'y');
});
})->get();
Again, just expanding aowie1's answer, which I've upvoted, as it's correct - this is just some additional info regarding nested queries with or conditions :)
Upvotes: 2