我正在通过过滤器进行产品搜索:
我的代码:
->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
您可以只在核心中搜索
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;
}
看它有第三个布尔参数。祝你好运。
Laravel 中有一个
orWhereIn
函数。它采用与 whereIn
函数相同的参数。
它不在文档中,但您可以在 Laravel API 中找到它。 请参阅 Laravel 8 或WhereIn 文档。
这应该给你这个:
$query-> orWhereIn('products.value', $f);
$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;
//输出@输入$stakeholderId = 1
//从
dms_stakeholder_permissionss
选择 group_concat(dms_stakeholder_permissions.fid) 作为 fid,rights
.dms_stakeholder_permissions
,其中 dms_stakeholder_permissions
.stakeholder_id
= 4 或 (dms_stakeholder_permissions
.stakeholder_id
= 1 和 dms_stakeholder_permissions
。 rights
(1, 2, 3))
是的,
orWhereIn
是一个你可以使用的方法。
我相当确定它应该给你你正在寻找的结果,但是,如果没有,你可以简单地使用
implode
创建一个字符串,然后分解它(这是对你的数组结构的猜测) :
$values = implode(',', array_map(function($value)
{
return trim($value, ',');
}, $filters));
$query->whereIn('products.value', explode(',' $values));
是的,
orWhereIn
Laravel 中存在 where 子句方法。请参阅以下官方 Laravel 查询生成器文档链接以获取详细信息。
文档链接: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))
例如,如果您有多个 whereIn OR 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();
示例 1: Eloquent 其中使用不同列名的 Laravel 模型进行查询
public function index()
{
$data= User::whereIn('name', ['john','dam','smith'])->get();
dd($data);
}
示例2:使用Wherein按类别过滤帖子 假设您有一个包含 Category_id 列的 posts 表,并且您想要从特定类别中检索帖子。您有一系列要定位的类别 ID:
$categoryIds = [2, 4, 6];
$posts = Post::whereIn('category_id', $categoryIds)->get();
示例 3: 使用 EloquentWherein 查询通过 ID 检索用户 假设您有一个用户表,并且您想通过 ID 检索用户。您有一系列您感兴趣的 ID:
$userIds = [1, 3, 5, 7];
$users = User::whereIn('id', $userIds)->get();
查看这里更详细。 https://woolocker.com/page/how-to-laravel-eloquent-wherein