如何查询具有多个表单输入的搜索

时间:2019-04-22 01:56:58

标签: laravel

我有一个具有多个输入字段的搜索功能,只要输入之一为空,它就会返回错误。

我尝试过这段代码,但是它只是返回查询语句,而不是结果本身

{{1}}

我希望输出是查询的结果

3 个答案:

答案 0 :(得分:1)

我认为您应该使用此代码

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

希望这可以解决您的问题。

答案 1 :(得分:1)

$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 = $employees->andWhere('job_requests.job_name','like',"%$job_name%");
  }
  if (isset($location) && !empty($location)) {
     $employees = $employees->andWhere('job_requests.location','=',$location);
  }
  if (isset($gender) && !empty($gender)) {
   $employees =  $employees->andWhere('user_infos.gender','=',$gender);
  }
  if (isset($salary) && !empty($salary)) {
    $employees = $employees->andWhere('job_requests.salary','<=',$salary);
  }
  if (isset($start_date) && !empty($start_date) && isset($end_date) && !empty($end_date)) {
    $employees = $employees->whereBetween('end_date',array($start_date,$end_date));
  }
  $employees->get();

答案 2 :(得分:0)

请尝试指定表end_date所在的字段。