From 3f8ca0c6d98b1616bb22306b09c1a2c3ca362f38 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Wed, 22 Apr 2026 14:14:34 +0700 Subject: [PATCH] fix: add exception handling to ViewServiceProvider boot method to prevent application crashes on database errors --- app/Providers/ViewServiceProvider.php | 41 ++++++++++++++------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/app/Providers/ViewServiceProvider.php b/app/Providers/ViewServiceProvider.php index 6f9efa7..8e4d65f 100644 --- a/app/Providers/ViewServiceProvider.php +++ b/app/Providers/ViewServiceProvider.php @@ -25,28 +25,31 @@ public function register(): void */ public function boot(): void { - if (Schema::hasTable('settings')) { - view()->share('general', app(GeneralSettings::class)); - view()->share('social', app(SocialMediaSettings::class)); - view()->share('seo', app(SeoSettings::class)); + try { + if (Schema::hasTable('settings')) { + view()->share('general', app(GeneralSettings::class)); + view()->share('social', app(SocialMediaSettings::class)); + view()->share('seo', app(SeoSettings::class)); - view()->composer('components.partials.footer', function ($view) { - $routeStatisticModel = config('route-statistics.model', Visitor::class); + view()->composer('components.partials.footer', function ($view) { + $routeStatisticModel = config('route-statistics.model', Visitor::class); - $today = $routeStatisticModel::whereDate('date', Carbon::today())->sum('counter'); - $yesterday = $routeStatisticModel::whereDate('date', Carbon::yesterday())->sum('counter'); - $thisMonth = $routeStatisticModel::whereMonth('date', Carbon::now()->month) - ->whereYear('date', Carbon::now()->year) - ->sum('counter'); - $total = $routeStatisticModel::sum('counter'); + $today = $routeStatisticModel::whereDate('date', Carbon::today())->sum('counter'); + $yesterday = $routeStatisticModel::whereDate('date', Carbon::yesterday())->sum('counter'); + $thisMonth = $routeStatisticModel::whereMonth('date', Carbon::now()->month) + ->whereYear('date', Carbon::now()->year) + ->sum('counter'); + $total = $routeStatisticModel::sum('counter'); - $view->with('visitorStats', [ - 'today' => number_format($today), - 'yesterday' => number_format($yesterday), - 'thisMonth' => number_format($thisMonth), - 'total' => number_format($total), - ]); - }); + $view->with('visitorStats', [ + 'today' => number_format($today), + 'yesterday' => number_format($yesterday), + 'thisMonth' => number_format($thisMonth), + 'total' => number_format($total), + ]); + }); + } + } catch (\Throwable $th) { } } }