2 回答
TA贡献2016条经验 获得超9个赞
我编写了更好版本的查询,但缺点是我必须将 status 或 Category_id 作为数组传递,如下所示 + 如果 city_id AND/OR District_id 为 null,则没有数据返回:
{
"city_id" : 5,
"district_id" : 9,
"category_id" : [5,4],
"status" : ["REJECTED"]
}
这是代码:
if ($cat != null && $status != null) {
$issues = $issues->where('city_id', $city)->where('district_id', $dist)
->where(function ($q) {
$q->whereIn('category_id', cleanString(request()->get('category_id')))
->orWhereIn('status', cleanString(request()->get('status')));
});
} elseif ($cat == "" || $cat == []) {
$issues = $issues->where('city_id', $city)->where('district_id', $dist)
->where(function ($q) {
$q->whereIn('status', cleanString(request()->get('status')));
});
} elseif ($status == "" || $status == []) {
$issues = $issues->where('city_id', $city)->where('district_id', $dist)
->where(function ($q) {
$q->whereIn('category_id', cleanString(request()->get('category_id')));
});
}
TA贡献1856条经验 获得超17个赞
强制转换为数组
$filters = [
'category_id' => (array) cleanString(request()->get('category_id', [])),
'status' => (array) cleanString(request()->get('status', [])),
];
$filters = array_filter($filters);
foreach($filters as $key => $val) {
$issues->orWhereIn($key, $val);
}
- 2 回答
- 0 关注
- 129 浏览
添加回答
举报