1$searchdata = ['A','B']; //keep it as an array
2if(count($searchdata) > 1) {
3 $data = User::with('tags')
4 ->orWhereHas('tags', function($q) use ($searchdata){
5 $q->whereIn('name', $searchdata);
6 })->get();
7
8} else {
9 $data = User::with('tags')
10 ->orWhereHas('tags', function($q) use ($searchdata){
11 $q->where('name', 'LIKE', '%' . $searchdata[0] . '%');
12 })->get();
13
14}
15
1
2$tagIDs = [1,2,3];
3$search = 'search string';
4
5 Post::where(function($q) use ($tagIDs, $search)
6 {
7 !$tagIDs ?: $q->whereIn('posts_tags.id_tag',$tagIDs)
8 ->havingRaw('count(DISTINCT posts_tags.id_tag) = '. count($tagIDs));
9
10 !$search ?: $q->where('title', 'LIKE','%'.$search.'%');
11
12 })
13 ->join('posts_tags','posts_tags.id_post, '=', 'post.id')
14 ->groupBy('post.id')
15 ->get()
16 ->toArray();
17
18