@@ -44,7 +44,7 @@ class Database extends Driver
4444 *
4545 * @var string
4646 */
47- protected $ encryptedKeys ;
47+ protected $ encrypted_keys ;
4848
4949 /**
5050 * Any query constraints that should be applied.
@@ -64,13 +64,13 @@ class Database extends Driver
6464 * @param \Illuminate\Database\Connection $connection
6565 * @param string $table
6666 */
67- public function __construct (Connection $ connection , $ table = null , $ key = null , $ value = null , $ encryptedKeys = [])
67+ public function __construct (Connection $ connection , $ table = null , $ key = null , $ value = null , $ encrypted_keys = [])
6868 {
6969 $ this ->connection = $ connection ;
7070 $ this ->table = $ table ?: 'settings ' ;
7171 $ this ->key = $ key ?: 'key ' ;
7272 $ this ->value = $ value ?: 'value ' ;
73- $ this ->encryptedKeys = $ encryptedKeys ?: [];
73+ $ this ->encrypted_keys = $ encrypted_keys ?: [];
7474 }
7575
7676 /**
@@ -166,16 +166,35 @@ public function forget($key)
166166 */
167167 protected function write (array $ data )
168168 {
169- $ keys = $ this ->newQuery ()->pluck ($ this ->key );
169+ // Get current data
170+ $ db_data = $ this ->newQuery ()->get ([$ this ->key , $ this ->value ])->toArray ();
170171
171172 $ insert_data = LaravelArr::dot ($ data );
172173 $ update_data = [];
173174 $ delete_keys = [];
174175
175- foreach ($ keys as $ key ) {
176+ foreach ($ db_data as $ db_row ) {
177+ $ key = $ db_row ->key ;
178+ $ value = $ db_row ->value ;
179+
180+ $ is_in_insert = $ is_different_in_db = $ is_same_as_fallback = false ;
181+
176182 if (isset ($ insert_data [$ key ])) {
177- $ update_data [$ key ] = $ insert_data [$ key ];
183+ $ is_in_insert = true ;
184+ $ is_different_in_db = (string ) $ insert_data [$ key ] != (string ) $ value ;
185+ $ is_same_as_fallback = $ this ->isEqualToFallback ($ key , $ insert_data [$ key ]);
186+ }
187+
188+ if ($ is_in_insert ) {
189+ if ($ is_same_as_fallback ) {
190+ // Delete if new data is same as fallback
191+ $ delete_keys [] = $ key ;
192+ } elseif ($ is_different_in_db ) {
193+ // Update if new data is different from db
194+ $ update_data [$ key ] = $ insert_data [$ key ];
195+ }
178196 } else {
197+ // Delete if current db not available in new data
179198 $ delete_keys [] = $ key ;
180199 }
181200
@@ -219,6 +238,11 @@ protected function prepareInsertData(array $data)
219238 foreach ($ data as $ key => $ value ) {
220239 $ value = $ this ->prepareValue ($ key , $ value );
221240
241+ // Don't insert if same as fallback
242+ if ($ this ->isEqualToFallback ($ key , $ value )) {
243+ continue ;
244+ }
245+
222246 $ db_data [] = array_merge (
223247 $ this ->getExtraColumns (),
224248 [$ this ->key => $ key , $ this ->value => $ value ]
@@ -228,6 +252,11 @@ protected function prepareInsertData(array $data)
228252 foreach ($ data as $ key => $ value ) {
229253 $ value = $ this ->prepareValue ($ key , $ value );
230254
255+ // Don't insert if same as fallback
256+ if ($ this ->isEqualToFallback ($ key , $ value )) {
257+ continue ;
258+ }
259+
231260 $ db_data [] = [$ this ->key => $ key , $ this ->value => $ value ];
232261 }
233262 }
@@ -247,9 +276,8 @@ protected function prepareInsertData(array $data)
247276 */
248277 protected function prepareValue (string $ key , $ value )
249278 {
250-
251279 // Check if key should be encrypted
252- if (in_array ($ key , $ this ->encryptedKeys )) {
280+ if (in_array ($ key , $ this ->encrypted_keys )) {
253281 // Cast to string to avoid error when a user passes a boolean value
254282 return Crypt::encryptString ((string ) $ value );
255283 }
@@ -268,9 +296,8 @@ protected function prepareValue(string $key, $value)
268296 */
269297 protected function unpackValue (string $ key , $ value )
270298 {
271-
272299 // Check if key should be encrypted
273- if (in_array ($ key , $ this ->encryptedKeys )) {
300+ if (in_array ($ key , $ this ->encrypted_keys )) {
274301 // Cast to string to avoid error when a user passes a boolean value
275302 return Crypt::decryptString ((string ) $ value );
276303 }
0 commit comments