2 回答

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊
我編寫了更好版本的查詢,但缺點(diǎn)是我必須將 status 或 Category_id 作為數(shù)組傳遞,如下所示 + 如果 city_id AND/OR District_id 為 null,則沒(méi)有數(shù)據(jù)返回:
{
"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貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊
強(qiáng)制轉(zhuǎn)換為數(shù)組
$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 關(guān)注
- 203 瀏覽
添加回答
舉報(bào)