【问题标题】:Multiple Where Has Query in Lumen / Laravel ProblemLumen / Laravel问题中的多个查询
【发布时间】:2020-07-29 10:53:27
【问题描述】:

我只是想在这种特殊情况下寻求帮助。因此,在我的应用程序中,我们希望有一个全局搜索功能,您可以在其中放置 1 个字符串,然后我们必须使用 laravel eloquent 在数据库中的多种情况下搜索该字符串,但这已经成为问题,因为我有多个 where has 然后我有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'] 返回一些东西,这就是问题的全部,希望它能澄清一些理解问题

【问题讨论】:

  • 您不必使用$data = $data-&gt;where('submission_date','&gt;=',$value);,而只需使用$data-&gt;where('submission_date','&gt;=',$value);
  • 很抱歉,您的回答没有回答我的问题
  • 这就是我评论的原因,但没有回答,你的问题到底是什么?
  • 我添加了一个段落,可能是为了消除一些误解?

标签: php laravel eloquent lumen eloquent-relationship


【解决方案1】:

我找到了解决这个问题的方法,我必须用 has 替换 whereHas 语法,它可以具有我在这种情况下需要的 OR 逻辑。

谢谢

【讨论】:

    猜你喜欢
    • 2016-12-27
    • 2015-10-20
    • 1970-01-01
    • 2019-02-23
    • 2016-04-07
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多