@lonad79190 Here you can try this :
class Post extends Model
{
public function tags()
{
return $this->belongsToMany(Tag::class);
}
public function scopeWithCountAndFindInSet($query, $tags)
{
$subQuery = $this->tags()
->select(DB::raw('COUNT(*)'))
->whereRaw('FIND_IN_SET(tags.id, ?)', [$tags])
->getQuery();
return $query->withCount(['tags' => $subQuery]);
}
}
you can call above method with calling bellow lines :
$tags = '1,2,3';
$posts = Post::withCountAndFindInSet($tags)->get();
I have used these tables in my case you can replace your tables with quizzes with post & categories with tags