withCount not working with Cache::remember

Posted 8 months ago by jorgeyoma


I have a function in PostController that searches for the tags that have been used in Posts and gives this data to the view.

I want to cache this info but now it gives me all the tags, no matter if they are used or not.

This works and gives me only the tags with posts.

        $tags =  Tag::withCount(['posts' => function ($query) {

This gives me all the tags. What's wrong?

$tags = Cache::remember('tags', 3600, function () {

       return  Tag::withCount(['posts' => function ($query) {


Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.