我通过过滤器搜索产品:
我的代码:
->where(function($query) use($filter)
{
if(!empty($filter)){
foreach ($filter as $key => $value) {
$f = explode(",", $value);
$query-> whereIn('products.value', $f);
}
}
})
查询:
and (products.value in (Bomann, PHILIPS) and products.value in (red,white))
但我需要:
and (products.value in (Bomann, PHILIPS) OR products.value in (red,white))
Laravel有类似的东西:
orWhereIn
答案 0 :(得分:32)
您可以在核心中搜索whereIn
函数来查看。这个给你。这必须回答你的所有问题
/**
* Add a "where in" clause to the query.
*
* @param string $column
* @param mixed $values
* @param string $boolean
* @param bool $not
* @return \Illuminate\Database\Query\Builder|static
*/
public function whereIn($column, $values, $boolean = 'and', $not = false)
{
$type = $not ? 'NotIn' : 'In';
// If the value of the where in clause is actually a Closure, we will assume that
// the developer is using a full sub-select for this "in" statement, and will
// execute those Closures, then we can re-construct the entire sub-selects.
if ($values instanceof Closure)
{
return $this->whereInSub($column, $values, $boolean, $not);
}
$this->wheres[] = compact('type', 'column', 'values', 'boolean');
$this->bindings = array_merge($this->bindings, $values);
return $this;
}
看看它有第三个布尔参数。祝你好运。
答案 1 :(得分:16)
Laravel中有orWhereIn
个功能。它采用与whereIn
函数相同的参数。
它不在文档中,但您可以在laravel API中找到它。 http://laravel.com/api/4.1/
那应该给你这个:
$query-> orWhereIn('products.value', $f);
答案 2 :(得分:6)
$query = DB::table('dms_stakeholder_permissions');
$query->select(DB::raw('group_concat(dms_stakeholder_permissions.fid) as fid'),'dms_stakeholder_permissions.rights');
$query->where('dms_stakeholder_permissions.stakeholder_id','4');
$query->orWhere(function($subquery) use ($stakeholderId){
$subquery->where('dms_stakeholder_permissions.stakeholder_id',$stakeholderId);
$subquery->whereIn('dms_stakeholder_permissions.rights',array('1','2','3'));
});
$result = $query->get();
return $result;
// OUTPUT @input $ stakeholderId = 1
//选择group_concat(dms_stakeholder_permissions.fid)为fid,dms_stakeholder_permissionss
。rights
来自dms_stakeholder_permissions
dms_stakeholder_permissions
。stakeholder_id
= 4或({{ 1}}。dms_stakeholder_permissions
= 1和stakeholder_id
。dms_stakeholder_permissions
in(1,2,3))
答案 3 :(得分:4)
是的,orWhereIn
是您可以使用的方法。
我很确定它应该为您提供所需的结果,但是,如果不是,您只需使用implode
创建一个字符串然后将其爆炸(这是猜测你的数组结构):
$values = implode(',', array_map(function($value)
{
return trim($value, ',');
}, $filters));
$query->whereIn('products.value', explode(',' $values));
答案 4 :(得分:0)
例如,如果你有多个whereIn或whereIn条件而你想放括号,那就这样做:
$getrecord = DiamondMaster::where('is_delete','0')->where('user_id',Auth::user()->id);
if(!empty($request->stone_id))
{
$postdata = $request->stone_id;
$certi_id =trim($postdata,",");
$getrecord = $getrecord->whereIn('id',explode(",", $certi_id))
->orWhereIn('Certi_NO',explode(",", $certi_id));
}
$getrecord = $getrecord->get();
答案 5 :(得分:0)
是的,orWhereIn
Laravel 中存在 where 子句方法。有关详细信息,请参阅以下 Laravel Query Builder 官方文档链接。
文档链接:https://laravel.com/docs/8.x/queries#additional-where-clauses
有两种方法可以得到以下输出。
and (products.value in (Bomann, PHILIPS) OR products.value in (red,white))
orWhereIn
$query = Product::where('color', 'blue')
->whereIn('value', ['Bomann', 'PHILIPS'])
->orWhereIn('value', ['red', 'white'])
->get();
输出:
select * from `products` where `color` = 'blue' and `value` in (Bomann, PHILIPS) OR `value` in (red,white))
orWhere
和 whereIn
$query2 = Product::where('color', 'blue')
->whereIn('value', ['Bomann', 'PHILIPS'])
->orWhere(function ($query) {
$query->whereIn('value', ['Bomann', 'PHILIPS']);
})
->get();
输出:
select * from `products` where `color` = 'blue' and `value` in (Bomann, PHILIPS) OR (`value` in (red,white))