From 946455b8be66b9930c91a4ee1c4de65f77d445ff Mon Sep 17 00:00:00 2001 From: kvostyc <21daniel.ibrahim@gmail.com> Date: Fri, 3 Oct 2025 17:20:10 +0200 Subject: [PATCH] refactor: optimize user data retrieval by using loadCount for relationships --- app/Http/Controllers/UserController.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 03c288a..b29cabd 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -18,15 +18,17 @@ { public function show(User $user): JsonResponse { + $user->loadCount(['posts', 'followers', 'following']); + return response()->json([ 'id' => $user->id, 'username' => $user->username, 'email' => $user->email, 'email_verified' => $user->email_verified_at !== null, 'created_at' => $user->created_at->toISOString(), - 'posts_count' => $user->posts()->count(), - 'followers_count' => $user->followers()->count(), - 'following_count' => $user->following()->count(), + 'posts_count' => $user->posts_count, + 'followers_count' => $user->followers_count, + 'following_count' => $user->following_count, ]); }