Skip to content
Advertisement

Multiple LIKE clauses in Laravel Eloquent

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

Advertisement

Answer

Try this :

$array = ['web design', 'accounting']; 
$result = DB::table('jobs')
   ->where(function ($query) use($array) {
     foreach($array as $key) {
        $query->orWhere('skillsetNeeded', 'LIKE', "%$key%")
     }
   })
  ->get();
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement