I've searched and found similar posts but none of them are the same as my case. Seems the Laravel documents don't show a case like mine though my case is quite common in a where clause of database queries.
The where clause in my case is like the following and I can't get it to work with where() and orwhere() methods and tricks people suggested in other posts. Any info appreciated. Thanks.
where a = 1 and b = 2 and (c = 3 or c = 4) and (d = 5 or d = 6)
Another level of complication is that I can combine all 'and' conditions into one single array and feed it into one where() method, but I have to group 'or' conditions into separate arrays and how many groups are dynamically collected in the codes, such as the following.
$and = [['a', '=', 1], ['b', '=', 2], ...];
if($cond1) $or['or1'] = [['c', '=', 3], ['c', '=', 4], ...];
if($cond2) $or['or2'] = [['d', '=', 5], ['d', '=', 6], ...];
if($cond3) $or['or3'] = [['e', '=', 7], ['e', '=', 8], ...];
...
PS: I like to thank all who replied (posts or comments). I've seriously considered all suggestions and tried some that could be applied to my case, but unfortunately none of them worked. So my solution is just to compile a raw where clause as shown in the example I gave above and used whereRaw() method. It's straight forward to do and worked quite well.
You can do it like so
Model::where('a', '1')
->where('b', '2')
->where(function ($q) {
$q->where('c', '3')->orWhere('c', '4');
})
->where(function ($q) {
$q->where('d', '5')->orWhere('d', '5');
})->get();
Something like this should do the trick:
Model::where([
['a', '=', 1],
['b', '=', 2],
[['c', '=', 3], 'OR', ['c', '=', 4]],
[['d', '=', 5], 'OR', ['d', '=', 6]]
]);
If I got your question, then this receipe is for you :
$result = DB::table('table')
->where('a', 1)
->where('b', 2)
->where(function($query) {
$query->where('c', 3)
->orWhere('c', 4)
})
->where(function($query) {
$query->where('d', 5)
->orWhere('d', 6)
->get();