0

How do I say WHERE (a=x AND b=y) OR (c=z AND d=j)

For x , y , z .. are variables .

How can i do that with eloquent ?

2 Answers 2

1

for multiple where statements use:

$result = Model::whereRaw('(a = ? and b=?) or (c=? and d=?)', ['x','y','z','j'])->get();
Sign up to request clarification or add additional context in comments.

Comments

1

You can achieve this by passing a closure into the where() function of the query builder. This is covered in the advanced wheres section of the docs http://laravel.com/docs/5.0/queries#advanced-wheres

$results = MyModel::where(function($query) {
        $query->where('a', 'x')
              ->where('b', 'y');
    })->orWhere(function($query) {
        $query->where('c', 'z')
              ->where('d', 'j');
    })->get();

4 Comments

ah I see, well sadly I'm running 5.0 right now and don't have a project with 4.0 lying around, I'll take your word on it :D, it's just that I couldn't find any documentation about it earlier (before version 4.2) so wasn't sure if it would work

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.