cacheService = $cacheService; } public function getPostStatistics(): array { return $this->cacheService->remember('statistics:posts', function () { return [ 'total_posts' => Post::count(), 'posts_by_category' => $this->getPostsByCategory(), 'posts_by_author' => $this->getPostsByAuthor(), 'most_commented' => $this->getMostCommentedPosts(), 'recent_posts' => $this->getRecentPosts(), ]; }); } protected function getPostsByCategory(): array { return Category::withCount('posts') ->orderByDesc('posts_count') ->get() ->map(fn($category) => [ 'name' => $category->name, 'count' => $category->posts_count, ]) ->toArray(); } protected function getPostsByAuthor(): array { return User::withCount('posts') ->orderByDesc('posts_count') ->limit(10) ->get() ->map(fn($user) => [ 'name' => $user->name, 'count' => $user->posts_count, ]) ->toArray(); } protected function getMostCommentedPosts(): array { return Post::withCount('comments') ->orderByDesc('comments_count') ->limit(5) ->get() ->map(fn($post) => [ 'title' => $post->title, 'comments_count' => $post->comments_count, ]) ->toArray(); } protected function getRecentPosts(): array { return Post::latest() ->limit(5) ->get() ->map(fn($post) => [ 'title' => $post->title, 'created_at' => $post->created_at, ]) ->toArray(); } }