diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php index 14baa4d22a5c..22dde6e6f9d0 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php @@ -1308,7 +1308,7 @@ protected function isClassDeviable($key) protected function isClassSerializable($key) { return $this->isClassCastable($key) && - method_exists($this->parseCasterClass($this->getCasts()[$key]), 'serialize'); + method_exists($this->resolveCasterClass($key), 'serialize'); } /** diff --git a/tests/Integration/Database/DatabaseEloquentModelCustomCastingTest.php b/tests/Integration/Database/DatabaseEloquentModelCustomCastingTest.php index 474b5682fc7e..87133f6d4353 100644 --- a/tests/Integration/Database/DatabaseEloquentModelCustomCastingTest.php +++ b/tests/Integration/Database/DatabaseEloquentModelCustomCastingTest.php @@ -230,6 +230,7 @@ public function testWithCastableInterface() ]); $this->assertInstanceOf(ValueObject::class, $model->value_object_with_caster); + $this->assertSame(serialize(new ValueObject('hello')), $model->toArray()['value_object_with_caster']); $model->setRawAttributes([ 'value_object_caster_with_argument' => null, @@ -418,7 +419,7 @@ public function __construct(string $name) public static function castUsing(array $arguments) { - return new class(...$arguments) implements CastsAttributes + return new class(...$arguments) implements CastsAttributes, SerializesCastableAttributes { private $argument; @@ -440,6 +441,11 @@ public function set($model, $key, $value, $attributes) { return serialize($value); } + + public function serialize($model, $key, $value, $attributes) + { + return serialize($value); + } }; } }