filter from taggable laravel code example
Example 1: filter from taggable laravel
$searchdata = ['A','B']; //keep it as an array
if(count($searchdata) > 1) {
$data = User::with('tags')
->orWhereHas('tags', function($q) use ($searchdata){
$q->whereIn('name', $searchdata);
})->get();
} else {
$data = User::with('tags')
->orWhereHas('tags', function($q) use ($searchdata){
$q->where('name', 'LIKE', '%' . $searchdata[0] . '%');
})->get();
}
Example 2: filter from taggable laravel
$tagIDs = [1,2,3];
$search = 'search string';
Post::where(function($q) use ($tagIDs, $search)
{
!$tagIDs ?: $q->whereIn('posts_tags.id_tag',$tagIDs)
->havingRaw('count(DISTINCT posts_tags.id_tag) = '. count($tagIDs));
!$search ?: $q->where('title', 'LIKE','%'.$search.'%');
})
->join('posts_tags','posts_tags.id_post, '=', 'post.id')
->groupBy('post.id')
->get()
->toArray();