From 3d0886eaee58d6d244ba094805f1ba393b87e5b7 Mon Sep 17 00:00:00 2001
From: "google-labs-jules[bot]"
<161369871+google-labs-jules[bot]@users.noreply.github.com>
Date: Tue, 23 Jun 2026 05:56:35 +0000
Subject: [PATCH 1/2] Optimize relationship counting in User Info Cards
component
Replaced `Category::with(['posts' => ...])` with `Category::withCount(['posts' => ...])` to avoid eagerly loading entire post collections into memory when only the count is needed.
Also added `'posts'` to `$user->loadCount([...])` to avoid loading the user's post collection to count total posts.
Updated the Blade template references (`$category->posts->count()` and `$user->posts->count()`) to use the newly available Eloquent attributes (`$category->posts_count` and `$user->posts_count`).
Co-authored-by: yilanboy <27554321+yilanboy@users.noreply.github.com>
---
benchmark.php | 65 +++++++++++++++++++
.../users/\342\232\241info-cards.blade.php" | 11 ++--
2 files changed, 71 insertions(+), 5 deletions(-)
create mode 100644 benchmark.php
diff --git a/benchmark.php b/benchmark.php
new file mode 100644
index 00000000..a35b6b64
--- /dev/null
+++ b/benchmark.php
@@ -0,0 +1,65 @@
+make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();
+
+use App\Models\Category;
+use App\Models\User;
+use Illuminate\Support\Facades\DB;
+
+// Find a user with some posts
+$user = User::has('posts')->first();
+if (!$user) {
+ echo "No users with posts found.\n";
+ die();
+}
+
+$startMemory = memory_get_usage();
+$startTime = microtime(true);
+DB::enableQueryLog();
+
+$categories = Category::with([
+ 'posts' => function ($query) use ($user) {
+ $query->where('user_id', $user->id);
+ },
+])->get();
+
+$count = 0;
+foreach ($categories as $category) {
+ $count += $category->posts->count();
+}
+
+$endMemory = memory_get_usage();
+$endTime = microtime(true);
+
+echo "--- BEFORE OPTIMIZATION ---\n";
+echo "Queries: " . count(DB::getQueryLog()) . "\n";
+echo "Memory Used: " . number_format($endMemory - $startMemory) . " bytes\n";
+echo "Time: " . number_format(($endTime - $startTime) * 1000, 2) . " ms\n";
+echo "Total posts count sum: " . $count . "\n";
+
+DB::flushQueryLog();
+
+$startMemory2 = memory_get_usage();
+$startTime2 = microtime(true);
+
+$categories2 = Category::withCount([
+ 'posts' => function ($query) use ($user) {
+ $query->where('user_id', $user->id);
+ },
+])->get();
+
+$count2 = 0;
+foreach ($categories2 as $category) {
+ $count2 += $category->posts_count;
+}
+
+$endMemory2 = memory_get_usage();
+$endTime2 = microtime(true);
+
+echo "\n--- AFTER OPTIMIZATION ---\n";
+echo "Queries: " . count(DB::getQueryLog()) . "\n";
+echo "Memory Used: " . number_format($endMemory2 - $startMemory2) . " bytes\n";
+echo "Time: " . number_format(($endTime2 - $startTime2) * 1000, 2) . " ms\n";
+echo "Total posts count sum: " . $count2 . "\n";
diff --git "a/resources/views/components/users/\342\232\241info-cards.blade.php" "b/resources/views/components/users/\342\232\241info-cards.blade.php"
index da2b8a54..6acd00da 100644
--- "a/resources/views/components/users/\342\232\241info-cards.blade.php"
+++ "b/resources/views/components/users/\342\232\241info-cards.blade.php"
@@ -14,6 +14,7 @@
public function render()
{
$user = User::find($this->userId)->loadCount([
+ 'posts',
'posts as posts_count_in_this_year' => function ($query) {
$query->whereYear('created_at', date('Y'));
},
@@ -24,7 +25,7 @@ public function render()
->where('posts.user_id', $user->id)
->count();
- $categories = Category::with([
+ $categories = Category::withCount([
'posts' => function ($query) use ($user) {
$query->where('user_id', $user->id);
},
@@ -129,8 +130,8 @@ class="rounded-full size-36"
@php
- $barWidth = $category->posts->count()
- ? (int) (($category->posts->count() / $user->posts->count()) * 100)
+ $barWidth = $category->posts_count
+ ? (int) (($category->posts_count / $user->posts_count) * 100)
: 0.2;
@endphp
@@ -144,7 +145,7 @@ class="h-4 from-green-500 via-emerald-500 to-teal-500 transition-all duration-30
- {{ $category->posts->count() }}
+ {{ $category->posts_count }}
@endforeach
@@ -153,7 +154,7 @@ class="flex col-span-1 justify-end items-center text-lg font-semibold text-teal-
文章總數
- {{ $user->posts->count() }}
+ {{ $user->posts_count }}
篇
From 51612f2082eb9c9b95a8c247fdf3344ea45e0d54 Mon Sep 17 00:00:00 2001
From: Allen
Date: Tue, 23 Jun 2026 14:12:21 +0800
Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20remove=20`benchmark.php`=20and?=
=?UTF-8?q?=20optimize=20query=20handling=20in=20`=E2=9A=A1info-cards.blad?=
=?UTF-8?q?e.php`=20for=20category=20posts=20count?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
benchmark.php | 65 -------------------
.../users/\342\232\241info-cards.blade.php" | 11 ++--
2 files changed, 6 insertions(+), 70 deletions(-)
delete mode 100644 benchmark.php
diff --git a/benchmark.php b/benchmark.php
deleted file mode 100644
index a35b6b64..00000000
--- a/benchmark.php
+++ /dev/null
@@ -1,65 +0,0 @@
-make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();
-
-use App\Models\Category;
-use App\Models\User;
-use Illuminate\Support\Facades\DB;
-
-// Find a user with some posts
-$user = User::has('posts')->first();
-if (!$user) {
- echo "No users with posts found.\n";
- die();
-}
-
-$startMemory = memory_get_usage();
-$startTime = microtime(true);
-DB::enableQueryLog();
-
-$categories = Category::with([
- 'posts' => function ($query) use ($user) {
- $query->where('user_id', $user->id);
- },
-])->get();
-
-$count = 0;
-foreach ($categories as $category) {
- $count += $category->posts->count();
-}
-
-$endMemory = memory_get_usage();
-$endTime = microtime(true);
-
-echo "--- BEFORE OPTIMIZATION ---\n";
-echo "Queries: " . count(DB::getQueryLog()) . "\n";
-echo "Memory Used: " . number_format($endMemory - $startMemory) . " bytes\n";
-echo "Time: " . number_format(($endTime - $startTime) * 1000, 2) . " ms\n";
-echo "Total posts count sum: " . $count . "\n";
-
-DB::flushQueryLog();
-
-$startMemory2 = memory_get_usage();
-$startTime2 = microtime(true);
-
-$categories2 = Category::withCount([
- 'posts' => function ($query) use ($user) {
- $query->where('user_id', $user->id);
- },
-])->get();
-
-$count2 = 0;
-foreach ($categories2 as $category) {
- $count2 += $category->posts_count;
-}
-
-$endMemory2 = memory_get_usage();
-$endTime2 = microtime(true);
-
-echo "\n--- AFTER OPTIMIZATION ---\n";
-echo "Queries: " . count(DB::getQueryLog()) . "\n";
-echo "Memory Used: " . number_format($endMemory2 - $startMemory2) . " bytes\n";
-echo "Time: " . number_format(($endTime2 - $startTime2) * 1000, 2) . " ms\n";
-echo "Total posts count sum: " . $count2 . "\n";
diff --git "a/resources/views/components/users/\342\232\241info-cards.blade.php" "b/resources/views/components/users/\342\232\241info-cards.blade.php"
index 6acd00da..27a21fe8 100644
--- "a/resources/views/components/users/\342\232\241info-cards.blade.php"
+++ "b/resources/views/components/users/\342\232\241info-cards.blade.php"
@@ -25,11 +25,12 @@ public function render()
->where('posts.user_id', $user->id)
->count();
- $categories = Category::withCount([
- 'posts' => function ($query) use ($user) {
- $query->where('user_id', $user->id);
- },
- ])->get();
+ $categories = Category::query()
+ ->withCount([
+ 'posts' => function ($query) use ($user) {
+ $query->where('user_id', $user->id);
+ },
+ ])->get();
return $this->view([
'user' => $user,