Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Yiga's avatar
Level 3

Laravel Cache::remember() and Cache::get()

I have code like this

$peopleQuery = People::where('status', 'active');
$people['total'] = $peopleQuery->count();
Cache::remember('people', 60, function () use ($people) {
            return $people;
        });
return Cache::get('people');

The cache is not working as expected. The question is when I use Cache::remember should I use Cache::get() because from the documentation it seems not? Thanks for the answer.

0 likes
16 replies
bugsysha's avatar
bugsysha
Best Answer
Level 61
$peopleCount = Cache::remember('people', 60, function () {
  return  People::where('status', 'active')->count();
});
$transactions['total'] = $peopleCount;
// or
$transactions['total'] = Cache::get('people');
Yiga's avatar
Level 3

@bugsysha Sorry I edited the question. I had an error in it. Please check now.

tykus's avatar

The cache is not working as expected.

What does this mean @yiga ?

Yiga's avatar
Level 3

@tykus It's not caching if there is any word like that.

tykus's avatar

@yiga you are executing the query outside the cache callback; so it will run every time. As @bugsysha described, executing the query inside the callback will result in the query executing only whenever the cache expires!

Cache::remember('people', 60, function () {
	return People::where('status', 'active')->count();
});
return Cache::get('people');

As far as the different assignments you are making and returning the cache result 🤷‍♂️

Yiga's avatar
Level 3

@tykus So this will be the right thing to do if I get you right?

return Cache::remember('people', 60, function () {
	return People::where('status', 'active')->count();
});

And this will be a variation of it right?

$people = People::where('status', 'active')->count();
return Cache::remember('people', 60, function () use ($people) {
	return $people;
});
tykus's avatar

@Yiga the variation is an incorrect use of caching; you cache because you want to prevent executing the query. However, your second snippet will execute the query every time.

This is the appropriate approach:

return Cache::remember('people', 60, function () {
	return People::where('status', 'active')->count();
});
1 like
tykus's avatar

@Yiga no, you can cache anything.

The variation would work, but it makes no sense; why perform the expensive action (database query) every time?

Yiga's avatar
Level 3

@tykus Thanks a lot I do appreciate the time and effort.

1 like
Yiga's avatar
Level 3

@MichalOravec If we'll all understand the documentation then we don't need forums like this.

bugsysha's avatar

@Yiga understanding documentation should be the first thing you learn. Everything else is secondary. Especially considering that Laravel has easy and clear documentation.

I never learn to use tools, frameworks, libraries, etc. I only learn to interpret documentation.

Please or to participate in this conversation.