[英]How to use MY SQL IN operator with where clause in laravel 5.4?
Can any one help me with that issue please. 有人可以帮我解决这个问题吗?
I need to use MY-SQL (IN) operator within where clause in laravel 5.4, I have columns variable which is a variable length array holding columns names called "$cols" so I can not use "whereIn" cause I don't know the count of my conditions colomns here's my code 我需要在laravel 5.4的where子句中使用MY-SQL(IN)运算符,我有column变量,它是一个可变长度的数组,其中包含名为“ $ cols”的列名,因此我不能使用“ whereIn”,因为我不知道我的条件列的数量这是我的代码
conditions = [];
if(!is_null($cols)){
for($i = 0; $i < count($cols); $i++){
$vals = explode("-", $values[$i]);
$conditions[] = [$cols[$i], 'IN', $values[$i]];
}
}
$data = DB::table('my_table')->where($conditions)->orderBy('id','desc')->get();
so any one can help me how to do something like that 所以任何人都可以帮助我如何做这样的事情
Thaks 解冻
As per the documentation , you can use whereIn
, eg: 根据文档 ,您可以使用whereIn
,例如:
$data = DB::table('my_table')
->where($conditions)
->whereIn('something', [val1, val2, val3])
->orderBy('id','desc')
->get();
Try something like this 试试这个
$queryBuilder = DB::table('my_table');
if( ! empty($cols)){
for($i = 0; $i < count($cols); $i++) {
$vals = explode("-", $values[$i]);
// adding whereIn() per column
$queryBuilder->whereIn($cols[$i], $vals);
}
}
$data = $queryBuilder
->orderBy('id','desc')
->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.