diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 44878f7bf880..2d54b2c82e5e 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -2421,7 +2421,15 @@ public function __sleep() $this->classCastCache = []; $this->attributeCastCache = []; - return array_keys(get_object_vars($this)); + // When serializing the model, we may accidentally catch up some virtual properties. + // We will cast the model to a native array to skip them and then apply a function + // to clean each of the property names which is miles faster than using a regex. + return array_map( + fn ($key) => $key[0] === "\0" + ? substr($key, strpos($key, "\0", 1) + 1) + : $key, + array_keys((array) $this) + ); } /**