Skip to content

Commit 77389d8

Browse files
dotMavriQclaude
andcommitted
fix: N+1 query in BookSettings::recacheCovers()
Replace individual $book->update() calls with a single bulk UPDATE query. File deletions and job dispatches still loop as needed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d89d78f commit 77389d8

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

app/Livewire/Books/BookSettings.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,27 @@ public function recacheCovers(): void
8484
})
8585
->get();
8686

87-
$count = 0;
88-
8987
foreach ($books as $book) {
90-
// Delete existing local cover file if it exists
9188
if ($book->cover_url && str_starts_with($book->cover_url, '/storage/covers/')) {
9289
$filename = str_replace('/storage/', '', $book->cover_url);
9390
Storage::disk('public')->delete($filename);
9491
}
92+
}
9593

96-
// Clear cover_url
97-
$book->update(['cover_url' => null]);
94+
Book::query()
95+
->where('user_id', Auth::id())
96+
->where(function ($query) {
97+
$query->whereNotNull('isbn')
98+
->orWhereNotNull('isbn13');
99+
})
100+
->update(['cover_url' => null]);
98101

99-
// Dispatch job to fetch fresh cover
102+
foreach ($books as $book) {
100103
FetchBookCover::dispatch($book->id);
101-
$count++;
102104
}
103105

106+
$count = $books->count();
107+
104108
session()->flash('message', "Re-caching covers for {$count} book(s). This runs in the background.");
105109
}
106110

0 commit comments

Comments
 (0)