第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 復雜的過濾查詢

Laravel 復雜的過濾查詢

PHP
元芳怎么了 2023-12-15 15:41:43
我需要編寫由“status”、“category_id”、“city_id”組成的 API 查詢領域,而且有很多案例:1) status is an array => ['OPEN','CLOSED'] OR category_id is an array => [1,2,4] AND city_id = 7 (could be any integer)2) status is an array => ['OPEN','CLOSED'] OR category_id is an integer => 2 AND city_id = 7 (could be any integer)3) status is a string => 'OPEN' OR category_id is an array => [1,2,4] AND city_id = 7 (could be any integer)4) status is an array => ['OPEN','CLOSED'] AND city_id = 7 (could be any integer) 5) category_id is an array => [1,2,4] AND city_id = 7 (could be any integer) 6) status is a string => 'OPEN' AND city_id = 7 (could be any integer) 7) category_id is an integer => 1 AND city_id = 7 (could be any integer) 我已經嘗試編寫此查詢,但是,對語句數量感到困惑(代碼無法正常工作,還有district_id,但為了簡單起見,我沒有提到):        $cat = cleanString(request()->get('category_id'));        $status = cleanString(request()->get('status'));        $city = cleanString(request()->get('city_id'));        $dist = cleanString(request()->get('district_id'));        if ($cat != null && $status != null) {            if (is_array($cat) && is_array($status)) {                $issues = $issues->whereIn('category_id', $cat)->orWhereIn('status', $status)->where('city_id', $city)->where('district_id', $dist);            } elseif (is_array($cat)) {                $issues = $issues->whereIn('category_id', $cat)->where('status', $status)->where('city_id', $city)->where('district_id', $dist);            } elseif (is_array($status)) {                $issues = $issues->whereIn('status', $status)->where('category_id', $cat)->where('city_id', $city)->where('district_id', $dist);            } elseif (is_string($cat) && is_string($status)) {                $issues = $issues->where('category_id', $cat)->where('status', $status)->where('city_id', $city)->where('district_id', $dist);            }有沒有辦法不使用這么多 if-else 情況并使代碼看起來更干凈并正常工作? 提前感謝大家的解答!
查看完整描述

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')));

                    });

            }


查看完整回答
反對 回復 2023-12-15
?
慕慕森

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);

}


查看完整回答
反對 回復 2023-12-15
  • 2 回答
  • 0 關注
  • 180 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號