in order to have
Cache
class you need to import it
use Illuminate\Support\Facades\Cache;
$postCount = Cache::remember(
'count.posts.' . $user->id,
now()->addSeconds(30),
function () use ($user) {
return $user->posts->count();
});
in this case the post count will be cached with different key on every user and will be retrieved by the authorized users id
2nd argument is expiration date, in this case 30 seconds in the future
3rd argument is a callback of what to do and store if cache is not found (is not set yet or 30 seconds has passed)