From 37a821c682efaf77acfc68f09c04d260244b8e21 Mon Sep 17 00:00:00 2001 From: alexvenga Date: Sun, 11 Dec 2022 20:20:40 +0300 Subject: [PATCH] Add cache forget model observer --- src/LaravelSiteSettingsProvider.php | 8 +++++- src/Observers/LSSObserver.php | 38 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/Observers/LSSObserver.php diff --git a/src/LaravelSiteSettingsProvider.php b/src/LaravelSiteSettingsProvider.php index 0a09bb4..dc5a1c6 100644 --- a/src/LaravelSiteSettingsProvider.php +++ b/src/LaravelSiteSettingsProvider.php @@ -3,6 +3,9 @@ namespace VI\LaravelSiteSettings; use Illuminate\Support\ServiceProvider; +use VI\LaravelSiteSettings\Models\LaravelSiteSetting; +use VI\LaravelSiteSettings\Models\LaravelSiteSettingGroup; +use VI\LaravelSiteSettings\Observers\LSSObserver; class LaravelSiteSettingsProvider extends ServiceProvider { @@ -30,9 +33,12 @@ public function boot() __DIR__ . '/../stubs/MoonShine/Resources/LaravelSiteSettingResource.php.stub' => app_path('MoonShine/Resources/LaravelSiteSettingResource.php'), ], 'moonshine'); + $this->loadMigrationsFrom(__DIR__ . '/../database/migrations'); + } - $this->loadMigrationsFrom(__DIR__ . '/../database/migrations'); + LaravelSiteSetting::observe(LSSObserver::class); + LaravelSiteSettingGroup::observe(LSSObserver::class); } diff --git a/src/Observers/LSSObserver.php b/src/Observers/LSSObserver.php new file mode 100644 index 0000000..4a7eb56 --- /dev/null +++ b/src/Observers/LSSObserver.php @@ -0,0 +1,38 @@ +forget(config('laravelsitesettings.cache_key')); + } + + + public function updated(Model $model) + { + cache()->forget(config('laravelsitesettings.cache_key')); + } + + + public function deleted(Model $model) + { + cache()->forget(config('laravelsitesettings.cache_key')); + } + + + public function restored(Model $model) + { + cache()->forget(config('laravelsitesettings.cache_key')); + } + + + public function forceDeleted(Model $model) + { + cache()->forget(config('laravelsitesettings.cache_key')); + } +} \ No newline at end of file