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
.
CodePudding user response:
$query->where(function($query){
$columns = ['text1', 'text2', 'text3'];
foreach($columns as $column){
$query->OrWhere($column, '=', $_GET['lorem']);}});
This might work
CodePudding user response:
Try this...
$columns = ['text1', 'text2', 'text3'];
$query->where(function ($query) use ($columns) {
foreach ($columns as $key => $column) {
if ($key == 0) $query->where($column, '=', $_GET['lorem']);
else $query->OrWhere($column, '=', $_GET['lorem']);
}
});
CodePudding user response:
It looks like the documentation for this actually does allow you to pass an array of equal statements in a key-value arrangement.
$columns = [
'text1'=>$value,
'text2'=>$value,
'text3'=>$value
];
$query->where($columns);
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.