Is it possible to combine multiple orWhere()
inside one where()
?
E.g. The following:
$query->where(function($query){
$query->where('text1', '=', $_GET['lorem'])
->orWhere('text2', '=', $_GET['lorem'])
->orWhere('text3', '=', $_GET['lorem'])
->orWhere('text4', '=', $_GET['lorem']);
});
Would look something like this:
$query->where(function($query){
$query->where(['text1' || 'text2' || 'text3' || 'text4'], '=', $_GET['lorem']);
});
P.S. In the above hypothetical example, the [
]
aren’t really referring to an array but I’m just using the brackets to group text1/2/3/4
.
3
Answers
This might work
Try this…
It looks like the documentation for this actually does allow you to pass an array of equal statements in a key-value arrangement.
You can see this works from the laravel docs in 9.X
https://github.com/laravel/framework/blob/29430b413b29fb60073ad26682a572df2ab5f5b2/src/Illuminate/Database/Query/Builder.php#L703
This shows the builder where clause. Following this, are the lines 708-710 which take the array and make it into the looped where statement that has been a solution for you up until now.
Please note that this method seems to only work with
'='
as far as I can see.TLDR:
Put key-value array into first param of where method.