From d725c3199652bc4ce9b8bb8192bddc462e7e4e9e Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 3 Feb 2026 10:37:59 +0000 Subject: [PATCH 1/2] Optimize header notification count check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced `auth()->user()->unreadNotifications->count()` with `auth()->user()->unreadNotifications()->exists()` in `resources/views/components/layouts/⚡header.blade.php`. This prevents hydrating all unread notification models, significantly reducing memory usage for users with many notifications. Also consolidated the check into a single variable `$hasUnreadNotifications` to ensure only one query is executed per render. Fixed syntax errors in `User.php` and `Comment.php` related to method chaining on instantiation. Added `HeaderPerformanceTest.php` to verify the optimization. Co-authored-by: yilanboy <27554321+yilanboy@users.noreply.github.com> --- app/Models/Comment.php | 6 +- app/Models/User.php | 6 +- .../layouts/\342\232\241header.blade.php" | 8 ++- tests/Feature/HeaderPerformanceTest.php | 58 +++++++++++++++++++ 4 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 tests/Feature/HeaderPerformanceTest.php diff --git a/app/Models/Comment.php b/app/Models/Comment.php index 99071d7e..1b639cab 100644 --- a/app/Models/Comment.php +++ b/app/Models/Comment.php @@ -77,10 +77,12 @@ protected function hierarchy(): Attribute CommentHierarchy SQL; - return new Attribute( + $attribute = new Attribute( get: fn ($value) => Arr::first( DB::select($query, ['id' => $this->id]) ) - )->shouldCache(); + ); + + return $attribute->shouldCache(); } } diff --git a/app/Models/User.php b/app/Models/User.php index d3c5b7bc..2b5d7d8b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -81,9 +81,11 @@ public function notifyNewComment(NewComment $instance): void protected function gravatarUrl(): Attribute { - return new Attribute( + $attribute = new Attribute( get: fn ($value) => get_gravatar(email: $this->email, size: 512) - )->shouldCache(); + ); + + return $attribute->shouldCache(); } public function passkeys(): MorphMany diff --git "a/resources/views/components/layouts/\342\232\241header.blade.php" "b/resources/views/components/layouts/\342\232\241header.blade.php" index 8284cc54..c2847765 100644 --- "a/resources/views/components/layouts/\342\232\241header.blade.php" +++ "b/resources/views/components/layouts/\342\232\241header.blade.php" @@ -55,6 +55,10 @@ public function logout(Logout $logout): void @endscript +@php + $hasUnreadNotifications = auth()->check() ? auth()->user()->unreadNotifications()->exists() : false; +@endphp +