diff --git a/README.md b/README.md index 577e76c..4a7779f 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,17 @@ $siteTItle = Settings::get('site_title'); $siteTitle = Settings::get('site_title', 'Your Default Awesome Website'); ``` -### Forget a global setting +### Delete a global setting + +Now, if you want to delete the setting + +```php +use RuangDeveloper\LaravelSettings\Facades\Settings; + +Settings::delete('site_title'); +``` + +### Forget a global setting (deprecated, use delete instead) Now, if you want to delete the setting @@ -120,7 +130,14 @@ $isSubscribed = $user->getSetting('subscribe_newsletter', false); // ``` -**Forget a setting** +**Delete a setting** + +```php +$user = App\Models\User::find(1); +$user->deleteSetting('subscribe_newsletter'); +``` + +**Forget a setting (deprecated, use delete instead)** ```php $user = App\Models\User::find(1); diff --git a/src/Services/SettingsService.php b/src/Services/SettingsService.php index c6b5c1f..36bbca7 100644 --- a/src/Services/SettingsService.php +++ b/src/Services/SettingsService.php @@ -52,6 +52,7 @@ public function get(string $key, mixed $default = null): mixed /** * Forget a setting value. * + * @deprecated Use delete() instead. * @param string $key * @return void */ @@ -60,6 +61,17 @@ public function forget(string $key): void $this->deleteSetting($key); } + /** + * Delete a setting item. + * + * @param string $key + * @return void + */ + public function delete(string $key): void + { + $this->deleteSetting($key); + } + /** * Set a setting value with model. * @@ -91,6 +103,7 @@ public function getWithModel(string $key, string $modelType, mixed $modelId, mix /** * Forget a setting value with model. * + * @deprecated Use deleteWithModel() instead. * @param string $key * @param string $modelType * @param mixed $modelId @@ -101,6 +114,19 @@ public function forgetWithModel(string $key, string $modelType, mixed $modelId): $this->deleteSetting($key, $modelType, $modelId); } + /** + * Delete a setting item with model. + * + * @param string $key + * @param string $modelType + * @param mixed $modelId + * @return void + */ + public function deleteWithModel(string $key, string $modelType, mixed $modelId): void + { + $this->deleteSetting($key, $modelType, $modelId); + } + /** * Store a setting value. * diff --git a/src/Traits/HasSettings.php b/src/Traits/HasSettings.php index 6f0e186..f078a84 100644 --- a/src/Traits/HasSettings.php +++ b/src/Traits/HasSettings.php @@ -92,6 +92,7 @@ public function getSetting(string $key, mixed $default = null): mixed /** * Forget a setting value. * + * @deprecated Use deleteSetting() instead. * @param string $key * @return void */ @@ -109,4 +110,25 @@ public function forgetSetting(string $key): void Cache()->forget(Support::getCacheKey($key, $this->getMorphClass(), $this->getKey())); } } + + /** + * Delete a setting item + * + * @param string $key + * @return void + */ + public function deleteSetting(string $key): void + { + $this->settings()->where( + [ + config('laravel-settings.key_name') => $key, + config('laravel-settings.morph_type') => $this->getMorphClass(), + config('laravel-settings.morph_id') => $this->getKey(), + ] + )->delete(); + + if (config('laravel-settings.with_cache')) { + Cache()->forget(Support::getCacheKey($key, $this->getMorphClass(), $this->getKey())); + } + } }