From 053344ed9bb8c46f5c501f2d95af0f36b1cc60c7 Mon Sep 17 00:00:00 2001 From: Raj Siva-Rajah <5361908+binaryfire@users.noreply.github.com> Date: Fri, 2 Jan 2026 09:35:10 +0000 Subject: [PATCH] Fix getCasts() to include casts() method for non-incrementing models The getCasts() method was only calling $this->casts() for incrementing models. For non-incrementing models (those using HasUuids or HasUlids), only the $casts property was returned, ignoring the casts() method entirely. This caused casts defined via the casts() method to be ignored for UUID/ULID models, leading to issues like arrays not being JSON-encoded when saved. --- .../Eloquent/Concerns/HasAttributes.php | 2 +- .../Eloquent/Concerns/HasAttributesTest.php | 96 +++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 tests/Core/Database/Eloquent/Concerns/HasAttributesTest.php diff --git a/src/core/src/Database/Eloquent/Concerns/HasAttributes.php b/src/core/src/Database/Eloquent/Concerns/HasAttributes.php index 0c673d832..ebe428fc0 100644 --- a/src/core/src/Database/Eloquent/Concerns/HasAttributes.php +++ b/src/core/src/Database/Eloquent/Concerns/HasAttributes.php @@ -64,7 +64,7 @@ public function getCasts(): array return static::$castsCache[static::class] = array_merge([$this->getKeyName() => $this->getKeyType()], $this->casts, $this->casts()); } - return static::$castsCache[static::class] = $this->casts; + return static::$castsCache[static::class] = array_merge($this->casts, $this->casts()); } /** diff --git a/tests/Core/Database/Eloquent/Concerns/HasAttributesTest.php b/tests/Core/Database/Eloquent/Concerns/HasAttributesTest.php new file mode 100644 index 000000000..8a3b8b8c4 --- /dev/null +++ b/tests/Core/Database/Eloquent/Concerns/HasAttributesTest.php @@ -0,0 +1,96 @@ +getCasts(); + + $this->assertArrayHasKey('id', $casts); + $this->assertArrayHasKey('data', $casts); + $this->assertSame('array', $casts['data']); + } + + public function testGetCastsIncludesCastsMethodForNonIncrementingModels(): void + { + $model = new HasAttributesUuidModel(); + + $casts = $model->getCasts(); + + $this->assertArrayHasKey('data', $casts); + $this->assertSame('array', $casts['data']); + } + + public function testGetCastsMergesPropertyAndMethodForNonIncrementingModels(): void + { + $model = new HasAttributesMixedCastsModel(); + + $casts = $model->getCasts(); + + // From $casts property + $this->assertArrayHasKey('config', $casts); + $this->assertSame('array', $casts['config']); + + // From casts() method + $this->assertArrayHasKey('data', $casts); + $this->assertSame('array', $casts['data']); + } +} + +class HasAttributesIncrementingModel extends Model +{ + protected ?string $table = 'test_models'; + + protected function casts(): array + { + return [ + 'data' => 'array', + ]; + } +} + +class HasAttributesUuidModel extends Model +{ + use HasUuids; + + protected ?string $table = 'test_models'; + + protected function casts(): array + { + return [ + 'data' => 'array', + ]; + } +} + +class HasAttributesMixedCastsModel extends Model +{ + use HasUuids; + + protected ?string $table = 'test_models'; + + protected array $casts = [ + 'config' => 'array', + ]; + + protected function casts(): array + { + return [ + 'data' => 'array', + ]; + } +}