Laravel Eloquent Query Using WHERE with OR AND OR?

PhpLaravelEloquentLaravel Query-Builder

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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);
});

Solution 2 - Php

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);
});

Solution 3 - Php

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);
    }
});

Solution 4 - Php

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

Solution 5 - Php

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');

Solution 6 - Php

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();

Solution 7 - Php

$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);
        })

Solution 8 - Php

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();

Solution 9 - Php

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;    
}

Solution 10 - Php

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);
});

Solution 11 - Php

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);
    });
});

Solution 12 - Php

This works for me

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

Solution 13 - Php

You can use:

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

Solution 14 - Php

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.

Solution 15 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFarzherView Question on Stackoverflow
Solution 1 - PhprmobisView Answer on Stackoverflow
Solution 2 - PhpYilmazerhakanView Answer on Stackoverflow
Solution 3 - PhpSabrina LeggettView Answer on Stackoverflow
Solution 4 - PhpSamrong Prey KabbasView Answer on Stackoverflow
Solution 5 - PhpMd. Saidur Rahman MilonView Answer on Stackoverflow
Solution 6 - PhpashokView Answer on Stackoverflow
Solution 7 - PhpMajbah HabibView Answer on Stackoverflow
Solution 8 - PhpKrishnamoorthy AcharyaView Answer on Stackoverflow
Solution 9 - PhpLeonView Answer on Stackoverflow
Solution 10 - PhpparastooView Answer on Stackoverflow
Solution 11 - PhpSupun PraneethView Answer on Stackoverflow
Solution 12 - PhpDankyi Anno KwakuView Answer on Stackoverflow
Solution 13 - PhpEngr. Umar EjazView Answer on Stackoverflow
Solution 14 - PhpHedayatullah SarwaryView Answer on Stackoverflow
Solution 15 - PhpDanish MirzaView Answer on Stackoverflow