簡體   English   中英

Lumen / Laravel 問題中的多個 Where 有查詢

[英]Multiple Where Has Query in Lumen / Laravel Problem

我只是想在這種特定情況下尋求幫助。 因此,在我的應用程序中,我們希望進行全局搜索 function 放置 1 個字符串,然后我們必須使用 laravel Z4B4D28FC143CA344E48631AFF6183 在數據庫中的多種情況下搜索該字符串有withcount(我稍后會展示)。 有人可以幫我解決這個問題嗎? 將不勝感激

這是代碼

    $result = [];
    $sort_order = "DESC";
    $post_param = $request->json()->all();
    $filters = $post_param['filters'];

    if($post_param['sortOrder'] > 0)
    {
        $sort_order = "ASC";
    }

    $financing_applications   = $this->financing_application->model();

    $data                     = $financing_applications::with('financing_application_data.business_type','financing_product_approval.financing_product')->withCount('financing_application_attachments as attachment');

    foreach($filters as $key => $filter){
        $value = $filter['value'];
        if($value != ""){
            switch($key){
                case "start_date_range":
                    $data   = $data->where('submission_date','>=',$value);
                    break;

                case "end_date_range":
                    $data   = $data->where('submission_date','<=',$value);
                    break;

                case "status":
                    $data   = $data->where($key,"LIKE","%$value%");
                    break;

                case "attachment_count":
                    $data   = $data->having('attachment_count','=',$value);
                    break;

                case "company_name":
                case "telephone":
                case "city":
                    if($key == "telephone"){
                        $key = "personal_phone_no";
                    }
                    if($key == "city"){
                        $key = "company_city";
                    }
                    $data   = $data->whereHas('financing_application_data',function($query) use ($key,$value) {
                        $query->where($key,"LIKE","%$value%");
                    });
                    break;

                case "business_type":
                    $data->whereHas('financing_application_data.business_type',function($query) use ($key,$value){
                        $query->where('business_type_parent','LIKE',"%$value%");
                    });
                    break;

                case "loan_type":
                case "loan_partner":
                    $data->whereHas('financing_product_approval.financing_product',function($query) use ($key,$value){
                        $query->where($key,"LIKE","%$value%");
                    });
                    break;

                case "global": //This is the problem
                    $data   = $data->whereHas('financing_application_data.business_type',function($query) use ($key,$value){
                        $query->whereRaw("business_type_parent LIKE ? ",["%$value%"]);
                    });
                    $data   = $data->whereHas('financing_product_approval.financing_product',function($query) use ($key,$value){
                        $query->whereRaw("loan_type LIKE ? OR loan_partner LIKE ?",["%$value%","%$value%"]);
                    });
                    break;
            }
        }
    }

    $total_records  = $data->count();
    $result         = $data->orderBy($post_param['sortField'],$sort_order)->skip($post_param['first'])->take($post_param['rows'])->get();

    return [
        "financing_applications" => $result,
        "total_records" => $total_records,
        "message" => "",
        "status" => 200,
    ];

因此,在這種情況下,我的預期結果是能夠使用所有案例並將其組合到 switch 語句中的“全局”案例中。

有沒有人有同樣的問題並有解決方案? 上述全局不起作用,因為 where 和 whereHas 預期為 AND 而不是 OR... 我一直在尋找解決方案,但它太復雜了我不知道這個問題的確切關鍵字

這是您需要的一些信息

"laravel/lumen-framework": "5.3.*"

更新:如果你們中的一些人誤解了我的問題,我很抱歉,所以問題只出在“全局”的情況下,現在另一種情況在於“whereHas”語法來過濾關系。 如果全局應該能夠結合“Where”和“WhereHas”,我已經這樣做了,但是因為沒有“orWhereHas”(只要我知道),所以它將返回空,因為它識別為“AND”語句

這里我給你 json 有效載荷:

{
"filters": {
    "global": {
        "matchMode": "undefined",
        "type": "string",
        "value": "Man"
    },
    "start_date_range": {
        "matchMode": "undefined",
        "type": "date",
        "value": ""
    },
    "end_date_range": {
        "matchMode": "undefined",
        "type": "date",
        "value": ""
    },
    "company_name": {
        "matchMode": "undefined",
        "type": "string",
        "value": ""
    },
    "business_type": {
        "matchMode": "undefined",
        "type": "string",
        "value": ""
    },
    "telephone": {
        "matchMode": "undefined",
        "type": "string",
        "value": ""
    },
    "city": {
        "matchMode": "undefined",
        "type": "string",
        "value": ""
    },
    "attachment_count": {
        "matchMode": "undefined",
        "type": "string",
        "value": ""
    },
    "loan_type": {
        "matchMode": "undefined",
        "type": "string",
        "value": ""
    },
    "loan_partner": {
        "matchMode": "undefined",
        "type": "string",
        "value": ""
    },
    "status": {
        "matchMode": "undefined",
        "type": "string",
        "value": ""
    }
},
"first": 0,
"rows": 8,
"sortOrder": -1,
"sortField": "submission_date"
}

所以目標是讓 filter['global']['value'] 返回一些東西,這就是問題所在,希望它能澄清一些理解問題

我找到了解決這個問題的方法,我必須用 has 替換 whereHas 語法,它可以具有我在這種情況下需要的 OR 邏輯。

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM