拉拉维尔 :搜索查询过滤器



我的搜索中有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();

如果您想在stone_minstone_max之间搜索特定产品,请执行以下操作:

$request = json_decode($request->searchDetails, true);
$stoneMinMax = collect($request)->pluck('stone_min', 'stone_max');
Product::where(function($q) use($productId) ($stoneMinMax){
$q->where('product_id', $productId)
->whereBetween('stone_column', $stoneMinMax->toArray());
})->get();

相关内容

  • 没有找到相关文章

最新更新