Sql Laravel中的多重相似子句

Sql Laravel中的多重相似子句,sql,laravel,eloquent,Sql,Laravel,Eloquent,我的控制器中当前有以下代码: $JobSuggestions = DB::table('jobs') ->orwhere('skillsetNeeded','like','%Accounting%') ->orwhere('skillsetNeeded','like','%Web Design%')->get(); 上面的代码可以工作,但我想要的是从数组中获取“Accounting”和“Web Design”,然后循环遍历该数组,以便查询是动态的,而不是在控制器上进行硬编码

我的控制器中当前有以下代码:

$JobSuggestions = DB::table('jobs')
->orwhere('skillsetNeeded','like','%Accounting%')
->orwhere('skillsetNeeded','like','%Web Design%')->get(); 
上面的代码可以工作,但我想要的是从数组中获取“Accounting”和“Web Design”,然后循环遍历该数组,以便查询是动态的,而不是在控制器上进行硬编码

$array = ['web design', 'accounting']; 
$result = DB::table('jobs')
   ->where(function ($query) use($array) {
     foreach($array as $key) {
        $query->orWhere('skillsetNeeded', 'LIKE', "%$key%")
     }
   })
  ->get();
试试这个:

$array = ['web design', 'accounting']; 
$result = DB::table('jobs')
   ->where(function ($query) use($array) {
     foreach($array as $key) {
        $query->orWhere('skillsetNeeded', 'LIKE', "%$key%")
     }
   })
  ->get();

您需要将2个变量
$accounting
$design
作为数组传递到这里。您好!谢谢你的评论。但会计和网络设计只存在于一个阵列中;比如:$array=['webdesign','accounting'];我尝试了以下方法:$JobSuggestions=DB::table('jobs')->where(函数($JobSuggestions)使用($array){for($I=0;$IorWhere('skillsetNeeded','like','%'。$array[$I].%';})->get();但我只得到一个结果,它应该显示2pps,然后应该是where,而不是where
$JobSuggestions->where('skillsetNeeded'…
试着让我知道你的代码实际上在工作。我使用的是一个旧的迁移文件,所以我查询的数据集不工作,但当我迁移到新数据时它工作了。你需要将2个变量
$accounting
$design
作为数组传递到这里。您好!谢谢您的评论。但是计数,而webdesign只存在于一个数组中;比如:$array=['webdesign','accounting'];我尝试过:$jobAdvisions=DB::table('jobs')->where(函数($jobAdvisions)使用($array){for($I=0;$IorWhere('SkillSetRequired'、'like'、'%'.$array[$i].%';}})->get();但我只得到一个结果,它应该显示2个PP,然后它应该是where,而不是where
$JobSuggestions->where('skillsetNeeded'…
尝试让我知道您的代码实际工作。我使用的是旧的迁移文件,因此我查询的数据集不工作,但在迁移到新数据时它工作了