【发布时间】:2018-08-13 11:33:23
【问题描述】:
我的搜索中有 searchDetails 数组。我已经完成了所有其他搜索并在过滤器上获取数据,问题是当我使用 stone_min, stone_max 搜索时,如果只有一个 searchDetails 数组,我会获取数据,如果有两个数据则返回 null。我怎样才能做到这一点 ?我想用stone_min, stone_max 搜索特定的product_id。
我尝试了什么:
要搜索的数据示例:
{
"limit": 1000,
"offset":0,
"user_id": "",
"min_price": "",
"max_price": "",
"searchDetails": [
{
"product_id": "1",
"attributeId": [],
"is_exclude": "no",
"stone_min": "5",
"stone_max": "15"
},
{
"product_id": "2",
"attributeId": [],
"is_exclude": "no",
"stone_min": "100",
"stone_max": "500"
}
]
}
我获取所有数据的查询
$searchablePost = Post::with(['product','postattribute.attribute.category','user.userDetails'])
->whereIn('product_id', $userApprovalProductIDs)
->where('status', 'Active')
->whereIn('demand_or_supply', $demand_or_supply);
搜索最小-最大石头:
if (count($searchDetails) > 0) {
$j = 0;
foreach ($searchDetails as $value) {
if (strtolower(trim($value['is_exclude'])) == "no") {
$searchablePost = $searchablePost->where(function ($query) use ($value) {
if ($value['stone_min'] && $value['stone_max']) {
if (isset($j) && $j == 0){
$query->where('stone_min', '>=', $value['stone_min'])->where('stone_max', '<=', $value['stone_max'])->where("product_id", (int)$value['product_id']);
} else {
$query->where('stone_min', '>=', $value['stone_min'])->where('stone_max', '<=', $value['stone_max'])->where("product_id", (int)$value['product_id']);
}
}
});
}
}
}
$searchedPost = $searchablePost->offset($offset)->limit($limit)->orderBy('id','desc')->get();
【问题讨论】:
-
你想做什么?
-
@LeoinstanceofKelmendi 我想用
stone_min, stone_max值搜索特定的product_id
标签: laravel laravel-5.5