1

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

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

Comments

2

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.