首页 > 解决方案 > 使用 PHP Laravel 从 MySQL 中的多个表中搜索数据

问题描述

我目前正在尝试从我的 mysql 数据库中的多个表中实现搜索数据。

Education 表数据为CSE, HSCDiplomaJobCircular 表数据为BSc in CSE

现在我尝试搜索匹配CSE

我的代码是

$profileId = Auth::guard('company')->user()->id;
$user_id = Applyer::
           where('com_id','=',$profileId)
           ->where('circular_id','=',$id)
           ->pluck('user_id')[0];

$examination = Education::selectRaw('subject')->where('user_id','=',$user_id)->orderBy('id','desc')->get();

   foreach ($examination as $exam => $sub) {     
      $exam_list = JobCircular::where('education_requirements','LIKE', "% 
                  {$sub}%")->get();
      }

echo "<pre>";
print_r([$exam_list]);

当我期望的工作循环教育要求BScCSE教育表数据时CSE

标签: phplaravelsearchmatching

解决方案


$needed_exam_ids = [ ];

    $examination = 
    Education::where('user_id','=',$user_id)- 
    >orderBy('id','desc')->get();

   Foreach ($examination as $data) {     
        $exam_list = 
  JobCircular::where('education_requirements','LIKE', "% 
                  {$data->subject}%")->count();
       if($count > 0)
       {
           // get exam id if 'cse' exist
           array_push($needed_exam_ids,$data->id);
       }
      }

// the result you want.
$education_data = Education::whereIn('id',$needed_exam_ids)->get(); 

推荐阅读