Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2463,7 +2463,12 @@ public function offsetSet($offset, $value): void
*/
public function offsetUnset($offset): void
{
unset($this->attributes[$offset], $this->relations[$offset], $this->attributeCastCache[$offset]);
unset(
$this->attributes[$offset],
$this->relations[$offset],
$this->attributeCastCache[$offset],
$this->classCastCache[$offset]
);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
use Illuminate\Support\InteractsWithTime;
use Illuminate\Support\Stringable;
use Illuminate\Support\Uri;
use Illuminate\Tests\Database\stubs\TestCast;
use Illuminate\Tests\Database\stubs\TestValueObject;
use InvalidArgumentException;
use LogicException;
use Mockery as m;
Expand Down Expand Up @@ -2823,6 +2825,17 @@ public function testMergeCastsMergesCastsUsingArrays()
$this->assertEquals($model->getCasts()['bar'], 'MyClass:myArgumentA,myArgumentB');
}

public function testUnsetCastAttributes()
{
$model = new EloquentModelCastingStub;
$model->asToObjectCast = TestValueObject::make([
'myPropertyA' => 'A',
'myPropertyB' => 'B',
]);
unset($model->asToObjectCast);
$this->assertArrayNotHasKey('asToObjectCast', $model->getAttributes());
}

public function testUpdatingNonExistentModelFails()
{
$model = new EloquentModelStub;
Expand Down Expand Up @@ -3837,6 +3850,7 @@ protected function casts(): array
'asCustomEnumArrayObjectAttribute' => AsEnumArrayObject::of(StringStatus::class),
'singleElementInArrayAttribute' => [AsCollection::class],
'duplicatedAttribute' => 'int',
'asToObjectCast' => TestCast::class,
];
}

Expand Down
49 changes: 49 additions & 0 deletions tests/Database/stubs/TestCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Illuminate\Tests\Database\stubs;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;

class TestCast implements CastsAttributes
{
/**
* @param Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return TestValueObject|null
*/
public function get(Model $model, string $key, mixed $value, array $attributes)
{
if (! json_validate($value)) {
return null;
}
$value = json_decode($value, true);
if (! is_array($value)) {
return null;
}

return TestValueObject::make($value);
}

/**
* @param Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return array
*/
public function set(Model $model, string $key, mixed $value, array $attributes)
{
if (! $value instanceof TestValueObject) {
return [
$key => null,
];
}

return [
$key => json_encode($value->toArray()),
];
}
}
34 changes: 34 additions & 0 deletions tests/Database/stubs/TestValueObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Illuminate\Tests\Database\stubs;

class TestValueObject
{
private string $myPropertyA;
private string $myPropertyB;

public static function make(?array $test): self
{
$self = new self;
if (! empty($test['myPropertyA'])) {
$self->myPropertyA = $test['myPropertyA'];
}
if (! empty($test['myPropertyB'])) {
$self->myPropertyB = $test['myPropertyB'];
}

return $self;
}

public function toArray(): array
{
if (isset($this->myPropertyA)) {
$result['myPropertyA'] = $this->myPropertyA;
}
if (isset($this->myPropertyB)) {
$result['myPropertyB'] = $this->myPropertyB;
}

return $result ?? [];
}
}