多个 Where Has Query in Lumen / Laravel 问题
Multiple Where Has Query in Lumen / Laravel Problem
我只是想在这种特定情况下寻求帮助。因此,在我的应用程序中,我们希望有一个全局搜索功能,您可以在其中放置 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 语句中的 "global" 情况下。
有没有人遇到同样的问题并且有解决办法?
上面的全局不起作用,因为 where 和 whereHas 应该是 AND 不是 OR...我一直在寻找解决方案,但它太复杂了,我不知道这个问题的确切关键字
这是您需要的一些信息
"laravel/lumen-framework": "5.3.*"
更新:
如果你们中的一些人误解了我的问题,我很抱歉,所以问题只在于 case "global",现在与另一个 case 在于 "whereHas" 语法来过滤关系。如果全局应该能够组合 "Where" 和 "WhereHas",我已经这样做了,但是因为没有 "orWhereHas"(据我所知),所以它将 return 为空因为它识别为 "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"
}
所以目标是制作过滤器['global']['value']return,这就是所有问题,希望它能澄清一些理解问题
我找到了解决这个问题的方法,我必须用 has 替换 whereHas 语法,在这种情况下它可以有我需要的 OR 逻辑。
谢谢
我只是想在这种特定情况下寻求帮助。因此,在我的应用程序中,我们希望有一个全局搜索功能,您可以在其中放置 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 语句中的 "global" 情况下。
有没有人遇到同样的问题并且有解决办法? 上面的全局不起作用,因为 where 和 whereHas 应该是 AND 不是 OR...我一直在寻找解决方案,但它太复杂了,我不知道这个问题的确切关键字
这是您需要的一些信息
"laravel/lumen-framework": "5.3.*"
更新: 如果你们中的一些人误解了我的问题,我很抱歉,所以问题只在于 case "global",现在与另一个 case 在于 "whereHas" 语法来过滤关系。如果全局应该能够组合 "Where" 和 "WhereHas",我已经这样做了,但是因为没有 "orWhereHas"(据我所知),所以它将 return 为空因为它识别为 "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"
}
所以目标是制作过滤器['global']['value']return,这就是所有问题,希望它能澄清一些理解问题
我找到了解决这个问题的方法,我必须用 has 替换 whereHas 语法,在这种情况下它可以有我需要的 OR 逻辑。
谢谢