I'm trying to create simple search query in db with multiple conditions.
I would like to search in db posts checking tags attached to posts and check searching word in posts title.
So far I made query that checks multiple tags for posts, but I'm struggling now how to check multiple words in post title.
$search = $request['search'];
$searchArr = explode(' ',$request['search']);
$searchTitle = [];
foreach ($searchArr as $search){
$searchTitle[] = ['title','like', "%$search%"];
}
echo Post::with(['allTags'])->whereHas('allTags', function($query) use ($searchArr) {
for ($i=0; $i < count($searchArr); $i++) {
if($i==0) {
$query->Where('name', $searchArr[$i]);
}else{
$query->orWhere('name', $searchArr[$i]);
}
}
})->where(['published'=>1])->orWhere([['title','like', "%$search%"],['published','=','1']])->get();
As you see I use the "for" loop to check multiple tags but I can't find a way how to do it in this part of code ->orWhere([['title','like', "%$search%"],['published','=','1']])->get();
Could someone tell me how to do it?
Thank you.
->whereHas('allTags'closure: You don't need to use$query->where(for the first constraint, you can use$query->orWhere(for all constraints.