@@ -213,7 +213,7 @@ public function toRawArray(bool $onlyChanged = false, bool $recursive = false):
213
213
}
214
214
215
215
foreach ($ this ->attributes as $ key => $ value ) {
216
- if (! $ this ->hasChanged ($ key )) {
216
+ if (! $ this ->hasChangedAttributes ($ key )) {
217
217
continue ;
218
218
}
219
219
@@ -254,11 +254,34 @@ public function hasChanged(?string $key = null): bool
254
254
{
255
255
// If no parameter was given then check all attributes
256
256
if ($ key === null ) {
257
- return $ this ->original !== $ this -> attributes ;
257
+ return $ this ->hasChangedAttributes () ;
258
258
}
259
259
260
260
$ key = $ this ->mapProperty ($ key );
261
261
262
+ return $ this ->hasChangedAttributes ($ key );
263
+ }
264
+
265
+ /**
266
+ * Checks a attribute to see if it has changed since the entity
267
+ * was created. Or, without a parameter, checks if any
268
+ * attributes have changed.
269
+ *
270
+ * @param string|null $key key of $this->attributes
271
+ */
272
+ private function hasChangedAttributes (?string $ key = null ): bool
273
+ {
274
+ // If no parameter was given then check all attributes
275
+ if ($ key === null ) {
276
+ foreach ($ this ->attributes as $ key => $ value ) {
277
+ if ($ this ->isChanged ($ key )) {
278
+ return true ;
279
+ }
280
+ }
281
+
282
+ return false ;
283
+ }
284
+
262
285
// Key doesn't exist in either
263
286
if (! array_key_exists ($ key , $ this ->original ) && ! array_key_exists ($ key , $ this ->attributes )) {
264
287
return false ;
@@ -269,7 +292,7 @@ public function hasChanged(?string $key = null): bool
269
292
return true ;
270
293
}
271
294
272
- return $ this ->original [ $ key] !== $ this -> attributes [ $ key ] ;
295
+ return $ this ->isChanged ( $ key) ;
273
296
}
274
297
275
298
/**
@@ -489,7 +512,6 @@ public function __get(string $key)
489
512
if (method_exists ($ this , $ method )) {
490
513
$ result = $ this ->{$ method }();
491
514
}
492
-
493
515
// Otherwise return the protected property
494
516
// if it exists.
495
517
elseif (array_key_exists ($ key , $ this ->attributes )) {
@@ -508,6 +530,30 @@ public function __get(string $key)
508
530
return $ result ;
509
531
}
510
532
533
+ /**
534
+ * Get cast value from the data array.
535
+ *
536
+ * @return mixed|null
537
+ */
538
+ private function _getCastData (string $ key , array $ data )
539
+ {
540
+ $ result = null ;
541
+
542
+ if (array_key_exists ($ key , $ data )) {
543
+ $ result = $ this ->castAs ($ data [$ key ], $ key );
544
+ }
545
+
546
+ return $ result ;
547
+ }
548
+
549
+ /**
550
+ * Check if the key value is changed.
551
+ */
552
+ private function isChanged (string $ key ): bool
553
+ {
554
+ return $ this ->_getCastData ($ key , $ this ->original ) !== $ this ->_getCastData ($ key , $ this ->attributes );
555
+ }
556
+
511
557
/**
512
558
* Returns true if a property exists names $key, or a getter method
513
559
* exists named like for __get().
0 commit comments