3

I would like to make Sql request like this :

Get the ((Age > 10 AND Weight > 50) OR Name = Jack) AND Actif = 1

My Eloquent request look like this :

$user = DB::table('users')
    ->where('Age', '>=', 10)
    ->where('Weight', '>=', 50)
    ->orWhere('Name', 'Jack')
    ->where('Actif', 1)
    ->get();

But actually it will return the result Jack although this column Actif is set to 0.

1 Answer 1

5

If we look at nesting of where part so it will be like this:

$sites = DB::table('users')
    ->where(function($q) {                       // (
      $q->where(function($q){                    //   (
             $q->where('Age', '>=', 10)          //     Age > 10
               ->where('Weight', '>=', 50);      //     AND Weight > 50
        })                                       //   )
        ->orWhere('Name', 'Jack');               //   OR Name = Jack
    })                                           // )
    ->where('Actif', 1)                          // AND Actif = 1
    ->get();
Sign up to request clarification or add additional context in comments.

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.