laravel - 如何使用多个表单输入查询搜索
问题描述
我有一个具有多个输入字段的搜索函数,只要其中一个输入为空,它就会返回错误。
我已经尝试过这段代码,但它只是返回查询语句,而不是结果本身
$employees = DB::table('job_requests')
->select('job_requests.*','user_infos.*')
->leftJoin('user_infos','user_infos.user_id','=','job_requests.user_id')
->where('user_infos.role','=','0');
if ($job_name) {
$employees->where('job_requests.job_name','like',"%$job_name%");
}
if ($location) {
$employees->where('job_requests.location','=',$location);
}
if ($gender) {
$employees->where('user_infos.gender','=',$gender);
}
if ($salary) {
$employees->where('job_requests.salary','<=',$salary);
}
if ($start_date && $end_date) {
$employees->whereBetween('end_date',array($start_date,$end_date));
}
$employees->get();
我希望输出是查询的结果
解决方案
我认为您应该使用此代码
$employees = DB::table('job_requests')
->select('job_requests.*','user_infos.*')
->leftJoin('user_infos','user_infos.user_id','=','job_requests.user_id')
->where('user_infos.role','=','0');
if (isset($job_name) && !empty($job_name)) {
$employees->andWhere('job_requests.job_name','like',"%$job_name%");
}
if (isset($location) && !empty($location)) {
$employees->andWhere('job_requests.location','=',$location);
}
if (isset($gender) && !empty($gender)) {
$employees->andWhere('user_infos.gender','=',$gender);
}
if (isset($salary) && !empty($salary)) {
$employees->andWhere('job_requests.salary','<=',$salary);
}
if (isset($start_date) && !empty($start_date) && isset($end_date) && !empty($end_date)) {
$employees->whereBetween('end_date',array($start_date,$end_date));
}
$employees->get();
希望这能解决您的问题。
推荐阅读
- javascript - 当存在特定模式时,正则表达式去除特定字符
- r - 将许多 .tif 文件从 rstudio 中的解压缩或压缩文件上传到 rasterbrick
- c# - Chartjs 的 MongoDB 日志聚合
- php - 在php中将文件从一个文件夹复制到另一个文件夹
- mongodb - Permission denied error Change dbpath for mongodb in centos
- c# - 在 SQL 中模拟标志枚举(c#)的最佳方法是什么?
- gatling - 使用 Gatling 构建动态 json 请求
- wordpress - 购物车中价格后的 WooCommerce 单位(来自属性值)示例:$9.99/kg
- oracle - PL/SQL 中的随机日期
- spacy - PhraseMatcher 用于匹配带有单词的短语