Skip to content

Commit 9c80cf4

Browse files
[11.x] Fixes incorrectly serializing virtual properties (backport) (#57201)
1 parent 66d4c92 commit 9c80cf4

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2421,7 +2421,15 @@ public function __sleep()
24212421
$this->classCastCache = [];
24222422
$this->attributeCastCache = [];
24232423

2424-
return array_keys(get_object_vars($this));
2424+
// When serializing the model, we may accidentally catch up some virtual properties.
2425+
// We will cast the model to a native array to skip them and then apply a function
2426+
// to clean each of the property names which is miles faster than using a regex.
2427+
return array_map(
2428+
fn ($key) => $key[0] === "\0"
2429+
? substr($key, strpos($key, "\0", 1) + 1)
2430+
: $key,
2431+
array_keys((array) $this)
2432+
);
24252433
}
24262434

24272435
/**

0 commit comments

Comments
 (0)