diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 7afa59933416..bca7e9160598 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -2317,7 +2317,7 @@ public function offsetSet($offset, $value): void */ public function offsetUnset($offset): void { - unset($this->attributes[$offset], $this->relations[$offset]); + unset($this->attributes[$offset], $this->relations[$offset], $this->attributeCastCache[$offset]); } /** diff --git a/tests/Database/DatabaseConcernsHasAttributesTest.php b/tests/Database/DatabaseConcernsHasAttributesTest.php index 0ba3437fbe7a..ac931b034a26 100644 --- a/tests/Database/DatabaseConcernsHasAttributesTest.php +++ b/tests/Database/DatabaseConcernsHasAttributesTest.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Concerns\HasAttributes; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; use Mockery as m; use PHPUnit\Framework\TestCase; @@ -49,6 +50,17 @@ public function testCastingEmptyStringToArrayDoesNotError() $this->assertTrue(json_last_error() === JSON_ERROR_NONE); } + + public function testUnsettingCachedAttribute() + { + $instance = new HasCacheableAttributeWithAccessor(); + $this->assertEquals('foo', $instance->getAttribute('cacheableProperty')); + $this->assertTrue($instance->cachedAttributeIsset('cacheableProperty')); + + unset($instance->cacheableProperty); + + $this->assertFalse($instance->cachedAttributeIsset('cacheableProperty')); + } } class HasAttributesWithoutConstructor @@ -88,3 +100,21 @@ public function usesTimestamps(): bool return false; } } + +/** + * @property string $cacheableProperty + */ +class HasCacheableAttributeWithAccessor extends Model +{ + public function cacheableProperty(): Attribute + { + return Attribute::make( + get: fn () => 'foo' + )->shouldCache(); + } + + public function cachedAttributeIsset($attribute): bool + { + return isset($this->attributeCastCache[$attribute]); + } +}