Skip to content

Commit 9e22c7c

Browse files
committed
fix getOriginal with custom casts
1 parent 2e9eef2 commit 9e22c7c

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ protected function mutateAttribute($key, $value)
476476
protected function mutateAttributeForArray($key, $value)
477477
{
478478
$value = $this->isClassCastable($key)
479-
? $this->getClassCastableAttributeValue($key)
479+
? $this->getClassCastableAttributeValue($key, $value)
480480
: $this->mutateAttribute($key, $value);
481481

482482
return $value instanceof Arrayable ? $value->toArray() : $value;
@@ -540,7 +540,7 @@ protected function castAttribute($key, $value)
540540
}
541541

542542
if ($this->isClassCastable($key)) {
543-
return $this->getClassCastableAttributeValue($key);
543+
return $this->getClassCastableAttributeValue($key, $value);
544544
}
545545

546546
return $value;
@@ -550,18 +550,19 @@ protected function castAttribute($key, $value)
550550
* Cast the given attribute using a custom cast class.
551551
*
552552
* @param string $key
553+
* @param mixed $value
553554
* @return mixed
554555
*/
555-
protected function getClassCastableAttributeValue($key)
556+
protected function getClassCastableAttributeValue($key, $value)
556557
{
557558
if (isset($this->classCastCache[$key])) {
558559
return $this->classCastCache[$key];
559560
} else {
560561
$caster = $this->resolveCasterClass($key);
561562

562563
return $this->classCastCache[$key] = $caster instanceof CastsInboundAttributes
563-
? ($this->attributes[$key] ?? null)
564-
: $caster->get($this, $key, $this->attributes[$key] ?? null, $this->attributes);
564+
? $value
565+
: $caster->get($this, $key, $value, $this->attributes);
565566
}
566567
}
567568

tests/Integration/Database/DatabaseEloquentModelCustomCastingTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public function testBasicCustomCasting()
2727
$this->assertSame('TAYLOR', $unserializedModel->getAttributes()['uppercase']);
2828
$this->assertSame('TAYLOR', $unserializedModel->toArray()['uppercase']);
2929

30+
$model->syncOriginal();
31+
$model->uppercase = 'dries';
32+
$this->assertEquals('TAYLOR', $model->getOriginal('uppercase'));
33+
3034
$model = new TestEloquentModelWithCustomCast;
3135

3236
$model->address = $address = new Address('110 Kingsbrook St.', 'My Childhood House');

0 commit comments

Comments
 (0)