Skip to content

Commit 33d7f2f

Browse files
committed
add to tests
1 parent 7befbb6 commit 33d7f2f

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ protected function castUsingClass($key)
546546
return $this->classCastCache[$key];
547547
} else {
548548
return $this->classCastCache[$key] = forward_static_call(
549-
[$this->getCasts()[$key], 'fromModelAttributes'], $this, $key, $this->attributes
549+
[$this->getCasts()[$key], 'fromModelAttributes'], $this, $key, $this->attributes[$key] ?? null, $this->attributes
550550
);
551551
}
552552
}
@@ -702,7 +702,11 @@ protected function setClassCastableAttribute($key, $value)
702702
{
703703
if (is_null($value)) {
704704
$this->attributes = array_merge($this->attributes, array_map(
705-
function () { return null; }, $this->castToClass($key)->toModelAttributes($this)
705+
function () { return null; },
706+
forward_static_call(
707+
[$this->getCasts()[$key], 'toModelAttributes'],
708+
$this, $key, $this->{$key}, $this->attributes
709+
)
706710
));
707711

708712
unset($this->classCastCache[$key]);

tests/Integration/Database/DatabaseEloquentModelCustomCastingTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public function testBasicCustomCasting()
4141

4242
$this->assertEquals('117 Spencer St.', json_decode($model->toJson(), true)['address_line_one']);
4343
$this->assertEquals('My House', json_decode($model->toJson(), true)['address_line_two']);
44+
45+
$model->address = null;
46+
47+
$this->assertNull($model->toArray()['address_line_one']);
48+
$this->assertNull($model->toArray()['address_line_two']);
4449
}
4550
}
4651

@@ -72,7 +77,7 @@ public static function toModelAttributes($model, $key, $value, $attributes)
7277

7378
class AddressCaster
7479
{
75-
public static function fromModelAttributes($model, $key, $attributes)
80+
public static function fromModelAttributes($model, $key, $value, $attributes)
7681
{
7782
return new Address($attributes['address_line_one'], $attributes['address_line_two']);
7883
}

0 commit comments

Comments
 (0)