Skip to content

Commit cd9b8bd

Browse files
authored
Merge pull request #2 from ruangdeveloper/feat/delete-setting
feat: add delete method for delete setting
2 parents 1ed7a4b + 500a13c commit cd9b8bd

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,17 @@ $siteTItle = Settings::get('site_title');
6969
$siteTitle = Settings::get('site_title', 'Your Default Awesome Website');
7070
```
7171

72-
### Forget a global setting
72+
### Delete a global setting
73+
74+
Now, if you want to delete the setting
75+
76+
```php
77+
use RuangDeveloper\LaravelSettings\Facades\Settings;
78+
79+
Settings::delete('site_title');
80+
```
81+
82+
### Forget a global setting (deprecated, use delete instead)
7383

7484
Now, if you want to delete the setting
7585

@@ -120,7 +130,14 @@ $isSubscribed = $user->getSetting('subscribe_newsletter', false);
120130
//
121131
```
122132

123-
**Forget a setting**
133+
**Delete a setting**
134+
135+
```php
136+
$user = App\Models\User::find(1);
137+
$user->deleteSetting('subscribe_newsletter');
138+
```
139+
140+
**Forget a setting (deprecated, use delete instead)**
124141

125142
```php
126143
$user = App\Models\User::find(1);

src/Services/SettingsService.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function get(string $key, mixed $default = null): mixed
5252
/**
5353
* Forget a setting value.
5454
*
55+
* @deprecated Use delete() instead.
5556
* @param string $key
5657
* @return void
5758
*/
@@ -60,6 +61,17 @@ public function forget(string $key): void
6061
$this->deleteSetting($key);
6162
}
6263

64+
/**
65+
* Delete a setting item.
66+
*
67+
* @param string $key
68+
* @return void
69+
*/
70+
public function delete(string $key): void
71+
{
72+
$this->deleteSetting($key);
73+
}
74+
6375
/**
6476
* Set a setting value with model.
6577
*
@@ -91,6 +103,7 @@ public function getWithModel(string $key, string $modelType, mixed $modelId, mix
91103
/**
92104
* Forget a setting value with model.
93105
*
106+
* @deprecated Use deleteWithModel() instead.
94107
* @param string $key
95108
* @param string $modelType
96109
* @param mixed $modelId
@@ -101,6 +114,19 @@ public function forgetWithModel(string $key, string $modelType, mixed $modelId):
101114
$this->deleteSetting($key, $modelType, $modelId);
102115
}
103116

117+
/**
118+
* Delete a setting item with model.
119+
*
120+
* @param string $key
121+
* @param string $modelType
122+
* @param mixed $modelId
123+
* @return void
124+
*/
125+
public function deleteWithModel(string $key, string $modelType, mixed $modelId): void
126+
{
127+
$this->deleteSetting($key, $modelType, $modelId);
128+
}
129+
104130
/**
105131
* Store a setting value.
106132
*

src/Traits/HasSettings.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public function getSetting(string $key, mixed $default = null): mixed
9292
/**
9393
* Forget a setting value.
9494
*
95+
* @deprecated Use deleteSetting() instead.
9596
* @param string $key
9697
* @return void
9798
*/
@@ -109,4 +110,25 @@ public function forgetSetting(string $key): void
109110
Cache()->forget(Support::getCacheKey($key, $this->getMorphClass(), $this->getKey()));
110111
}
111112
}
113+
114+
/**
115+
* Delete a setting item
116+
*
117+
* @param string $key
118+
* @return void
119+
*/
120+
public function deleteSetting(string $key): void
121+
{
122+
$this->settings()->where(
123+
[
124+
config('laravel-settings.key_name') => $key,
125+
config('laravel-settings.morph_type') => $this->getMorphClass(),
126+
config('laravel-settings.morph_id') => $this->getKey(),
127+
]
128+
)->delete();
129+
130+
if (config('laravel-settings.with_cache')) {
131+
Cache()->forget(Support::getCacheKey($key, $this->getMorphClass(), $this->getKey()));
132+
}
133+
}
112134
}

0 commit comments

Comments
 (0)