Ad
LARAVEL 5.8 - Multiple Conditions For WHERE LIKE Clause Using Array In Foreach Not Giving Complete Results
I have a column in my DB table which has multiple comma separated values and I have a comma separated array below:
$possibleSubMods = ["Pre-Op","Perioperative","PREOP","Endoscopy Nurse","Endoscopy","Endoscopy Lab"];
And column have the following values:
column name: subModality
Rows:
Post Anesthesia Care Unit Nurse (PACU), Pre-Op
Pre-Op, Endoscopy Nurse
PREOP, Endoscopy Lab
My code is:
$jobs = DB::table('joblists')
->where(function ($query) use ($possibleSubMods ){
foreach ($possibleSubs2 as $subModality) {
return $query->where('subModality', 'like', "%" . $subModality . "%");
}
})->count();
It only returns rows having 'Pre-Op' in the subModality colum, remember the 'Pre-Op' is the first value placed in $possibleSubMods
array.
Please, suggest me the quick and robust solution, I am using Laravel 5.8! TIA!
Ad
Answer
You may use
$jobs = DB::table('joblists')->where(function ($query) use ($possibleSubMods ){
foreach ($possibleSubs2 as $key => $subModality) {
if ($key === 0)
$query->where('subModality', 'like', "%" . $subModality . "%");
else
$query->orWhere('subModality', 'like', "%" . $subModality . "%");
}
})->count();
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → I can't do a foreign key, constraint error
- → Setting a default value on settings form return null in Octobercms
- → Eloquent Multitable query
- → "Laravel 5.1" add user and project with userId
- → Image does not go in database with file name only tmp name saved?
- → Database backup with custom code in laravel 5 and get the data upto 10 rows from per table in database
- → Trait 'IlluminateFoundationBusDispatchesJobs' not found
- → Setting the maxlength of text in an element that is displayed
- → laravel check between 2 integer from database
- → how to retrieve image from database in laravel 5.1?
- → relationship for database Column type object
- → Carousel in Laravel 4 does not show as expected
Ad