Laravel Eloquent中的多个LIKE子句 [英] Multiple LIKE clauses in Laravel Eloquent

查看:327
本文介绍了Laravel Eloquent中的多个LIKE子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I currently have this code in my controller:

$JobSuggestions = DB::table('jobs')
->orwhere('skillsetNeeded','like','%Accounting%')
->orwhere('skillsetNeeded','like','%Web Design%')->get(); 

上面的代码有效,但是我想要的是获取会计"信息.和网页设计"从一个数组开始,然后循环遍历该数组,以便查询是动态的,而不是在控制器上进行硬编码的

The above code works, but what I want is to get "Accounting" and "Web Design" from an array, and just loop through that array so that the query is dynamic instead of hard coded on the controller

推荐答案

尝试一下:

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

这篇关于Laravel Eloquent中的多个LIKE子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆