withCount not working with Cache::remember

Posted 5 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.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.