From 8a6ab059f52eb3df347aa4a43b7ed51ce3d551a7 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Thu, 18 Dec 2025 17:46:13 +0700 Subject: [PATCH] =?UTF-8?q?feat(controller):=20=E2=9C=A8=20Tambahkan=20Err?= =?UTF-8?q?or=20Handling=20pada=20Pemuatan=20Kategori?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Menambahkan mekanisme penanganan error yang tangguh pada metode `index` di `DaftarPustakaController` untuk memastikan halaman tetap berfungsi meskipun terjadi kegagalan pada query database. - **Implementasi Try-Catch**: Membungkus query `CategoryDaftarPustaka::withCount('daftarPustaka')` di dalam blok `try-catch`. - **Pencatatan Error**: Jika terjadi `Exception` (misalnya, tabel relasi belum ada atau terjadi error SQL), pesan error akan dicatat menggunakan `Log::warning()` untuk kemudahan debugging. - **Mekanisme Fallback**: Apabila query `withCount` gagal, controller akan menjalankan query alternatif `CategoryDaftarPustaka::get()` yang mengambil data kategori tanpa jumlah relasi. Ini memastikan bahwa halaman daftar pustaka tetap dapat ditampilkan kepada pengguna. - **Penambahan Dependensi**: Menambahkan `use Exception;` dan `use Illuminate\Support\Facades\Log;` untuk mendukung fungsionalitas ini. --- app/Http/Controllers/DaftarPustakaController.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/DaftarPustakaController.php b/app/Http/Controllers/DaftarPustakaController.php index 2241607..d9b122a 100644 --- a/app/Http/Controllers/DaftarPustakaController.php +++ b/app/Http/Controllers/DaftarPustakaController.php @@ -7,6 +7,8 @@ use Illuminate\Http\Request; use Modules\Lpj\Models\CategoryDaftarPustaka; use Modules\Lpj\Services\DaftarPustakaService; use Modules\Lpj\Http\Requests\DaftarPustakaRequest; +use Exception; +use Illuminate\Support\Facades\Log; class DaftarPustakaController extends Controller { @@ -22,7 +24,15 @@ class DaftarPustakaController extends Controller */ public function index(Request $request) { - $categories = CategoryDaftarPustaka::all(); + // Get categories with count of daftar pustaka + try { + $categories = CategoryDaftarPustaka::withCount('daftarPustaka')->get(); + } catch (\Exception $e) { + // Handle jika tabel belum ada atau error lainnya + Log::warning('Error loading categories with count: ' . $e->getMessage()); + $categories = CategoryDaftarPustaka::get(); // Fallback tanpa count + } + $daftar_pustaka = $this->daftarPustaka->getAllDaftarPustaka($request); return view('lpj::daftar-pustaka.index', [