Farzher
Farzher

Reputation: 14593

Laravel Eloquent Query: Using WHERE with OR AND OR?

How do I say WHERE (a = 1 OR b =1 ) AND (c = 1 OR d = 1)

For more complicated queries am I supposed to use raw SQL?

Upvotes: 430

Views: 726102

Answers (16)

Debendra
Debendra

Reputation: 144

ModelName::where(function ($query) use ($a) {
    $query->where('a', '=', $a)->orWhereNull('a');

})
->where(function ($query) use ($b,$c) {
    $query->where('b', '=', $b)
          ->orWhere('c', '=', $c);
})
->where('d',$d);

OUTPUT QUERY:

SELECT * FROM ModelName WHERE (a='a' or a is null) AND (b='b' or c='c') AND d='d';

Upvotes: 9

afcyy
afcyy

Reputation: 111

If you are using PHP 7.4+ you can also use arrow functions. They automatically capture variables from the surrounding scope, so you don't need to use the use() keyword.

Model::query()
  ->where(fn($query) => $query->where("a", "=", $a)->orWhere("b", "=", $b))
  ->where(fn($query) => $query->where("c", "=", $c)->orWhere("d", "=", $d));

Upvotes: 1

Engr. Umar Ejaz
Engr. Umar Ejaz

Reputation: 116

You can use:

Model::where('table_column', 'value')->orWhere('table_column', 'value')->get()

Upvotes: 6

rmobis
rmobis

Reputation: 27012

Make use of Logical Grouping (Laravel 7.x/4.2). For your example, it'd be something like this:

Model::where(function ($query) {
    $query->where('a', '=', 1)
          ->orWhere('b', '=', 1);
})->where(function ($query) {
    $query->where('c', '=', 1)
          ->orWhere('d', '=', 1);
});

Upvotes: 716

Dankyi Anno Kwaku
Dankyi Anno Kwaku

Reputation: 1293

This works for me

$business = Model::where('model_id', $model_id1)->orWhere('model_id', $model_id2)->first();

Upvotes: 2

Hedayatullah Sarwary
Hedayatullah Sarwary

Reputation: 2844

For eloquent query builder the following query will not work:

MODELNAME::where('a', 1)->orWhere('b', 1)->where('c', 1)->orWhere('d', 1);

But you can use this code:

MODELNAME::where(function($a) {
    $a->where('a', 1)->orWhere('b', 1);
})->where(function($a) {
    $a->where('c', 1)->orWhere('d', 1);
});

You can use the second query and it will work perfectly.

Upvotes: 2

Sabrina Leggett
Sabrina Leggett

Reputation: 9494

Incase you're looping the OR conditions, you don't need the the second $query->where from the other posts (actually I don't think you need in general, you can just use orWhere in the nested where if easier)

$attributes = ['first'=>'a','second'=>'b'];

$query->where(function ($query) use ($attributes) 
{
    foreach ($attributes as $key=>value)
    {
        //you can use orWhere the first time, doesn't need to be ->where
        $query->orWhere($key,$value);
    }
});

Upvotes: 36

Supun Praneeth
Supun Praneeth

Reputation: 3160

Best way to use sql brackets use callback function in laravel eloquent.

YourModal::where(function ($q) {
    $q->where('a', 1)->orWhere('b', 1);
})->where(function ($q) {
    $q->where('c', 1)->orWhere('d', 1);
});

You don't have to use = symbol, it's come as the default

Lest say if you have a query that contain brackets inside a brackets

WHERE (a = 1 OR (b = 1 and c = 5))


YourModal::where(function ($q) {
    $q->where('a', 1)->orWhere(function($q2){
      $q2->where('b', 1)->where('c', 5);
    });
});

lest say you want to make values dynamics

YourModal::where(function ($q) use($val1, $val2) {
    $q->where('a', $val1)->orWhere(function($q2) use($val2){
      $q2->where('b', $val2)->where('c', $val2);
    });
});

Upvotes: 2

Krishnamoorthy Acharya
Krishnamoorthy Acharya

Reputation: 4254

Another way without using Modal

Database: stocks Columns:id,name,company_name,exchange_name,status enter image description here

  $name ='aa'
  $stocks = DB::table('stocks')
            ->select('name', 'company_name', 'exchange_name')
            ->where(function($query) use ($name) {
                $query->where('name', 'like', '%' . $name . '%')
                ->orWhere('company_name', 'like', '%' . $name . '%');
            })
            ->Where('status', '=', 1)
            ->limit(20)
            ->get();

Upvotes: 10

parastoo
parastoo

Reputation: 2469

YourModel::where(function ($query) use($a,$b) {
    $query->where('a','=',$a)
          ->orWhere('b','=', $b);
})->where(function ($query) use ($c,$d) {
    $query->where('c','=',$c)
          ->orWhere('d','=',$d);
});

Upvotes: 6

Md. Saidur Rahman Milon
Md. Saidur Rahman Milon

Reputation: 2911

Simply Use in Laravel Eloquent:

$a='foo', $b='bar', $c='john', $d='doe';

Coder::where(function ($query) use ($a, $b) {
    $query->where('a', '=', $a)
          ->orWhere('b', '=', $b);
})->where(function ($query) use ($c, $d) {
    $query->where('c', '=', $c)
          ->orWhere('d', '=', $d);
})->get();

Will produce a query like:

SELECT * FROM <table> WHERE (a='foo' or b='bar') AND (c='john' or d='doe');

Upvotes: 18

Majbah Habib
Majbah Habib

Reputation: 8558

$a, $b, $c, $d can be dynamic values by the query

 ->where(function($query) use ($a, $b)
        {
            $query->where('a', $a)
                  ->orWhere('b',$b);
        })
 ->where(function($query) use ($c, $d)
        {
            $query->where('c', $c)
                  ->orWhere('d',$d);
        })

Upvotes: 8

Yilmazerhakan
Yilmazerhakan

Reputation: 1875

If you want to use parameters for a,b,c,d in Laravel 4

Model::where(function ($query) use ($a,$b) {
    $query->where('a', '=', $a)
          ->orWhere('b', '=', $b);
})
->where(function ($query) use ($c,$d) {
    $query->where('c', '=', $c)
          ->orWhere('d', '=', $d);
});

Upvotes: 139

Samrong Prey Kabbas
Samrong Prey Kabbas

Reputation: 454

if you want to use parentheses in laravel 4 and don't forget return
In Laravel 4 (at least) you need to use $a, $b in parentheses as in the example

$a = 1;
$b = 1;
$c = 1;
$d = 1;
Model::where(function ($query) use ($a, $b) {
    return $query->where('a', '=', $a)
          ->orWhere('b', '=', $b);
})->where(function ($query) use ($c, $d) {
    return $query->where('c', '=', $c)
          ->orWhere('d', '=', $d);
});

This is my result: enter image description here

Upvotes: 36

ashok
ashok

Reputation: 91

You can also query the first or condition and later you can apply another or condition

$model = Model::where('a',1)->orWhere('b',1);

now apply another condition on that $model variable

$model1 = $model->where('c',1)->orWhere('d',1)->get();

Upvotes: 9

Leon
Leon

Reputation: 1853

You can also use query scopes to make things a bit tidier, so you can do something like:

Invoice::where('account', 27)->notPaidAt($date)->get();

Then in your model

public function scopeNotPaidAt($query, $asAt)
{
    $query = $query->where(function ($query) use ($asAt) { 
        $query->where('paid', '=', '0000-00-00')->orWhere('paid', '>=', $asAt); 
    });
    return $query;    
}

Upvotes: 5

Related Questions