Skip to content

Commit 899c765

Browse files
committed
adjust getoriginal
1 parent b20125d commit 899c765

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

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

+15-5
Original file line numberDiff line numberDiff line change
@@ -1185,12 +1185,22 @@ public function setRawAttributes(array $attributes, $sync = false)
11851185
*/
11861186
public function getOriginal($key = null, $default = null)
11871187
{
1188-
if ($key && $this->isClassCastable($key) && $this->isDirty()) {
1189-
return (new static)->setRawAttributes(
1190-
$this->original, $sync = true
1191-
)->getOriginal($key, $default);
1192-
}
1188+
return (new static)->setRawAttributes(
1189+
$this->original, $sync = true
1190+
)->getOriginalWithoutRewindingModel($key, $default);
1191+
}
11931192

1193+
/**
1194+
* Get the model's original attribute values.
1195+
*
1196+
* This should not be called by the end user / developer.
1197+
*
1198+
* @param string|null $key
1199+
* @param mixed $default
1200+
* @return mixed|array
1201+
*/
1202+
public function getOriginalWithoutRewindingModel($key = null, $default = null)
1203+
{
11941204
if ($key) {
11951205
return $this->transformModelValue(
11961206
$key, Arr::get($this->original, $key, $default)

tests/Integration/Database/DatabaseEloquentModelCustomCastingTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,26 @@ public function testGetOriginalWithCastValueObjects()
103103

104104
$model->address = new Address('117 Spencer St.', 'Another house.');
105105

106+
$this->assertEquals('117 Spencer St.', $model->address->lineOne);
106107
$this->assertEquals('110 Kingsbrook St.', $model->getOriginal('address')->lineOne);
107108
$this->assertEquals('117 Spencer St.', $model->address->lineOne);
108109

109110

111+
112+
$model = new TestEloquentModelWithCustomCast([
113+
'address' => new Address('110 Kingsbrook St.', 'My Childhood House')
114+
]);
115+
116+
$model->syncOriginal();
117+
118+
$model->address = new Address('117 Spencer St.', 'Another house.');
119+
120+
$this->assertEquals('117 Spencer St.', $model->address->lineOne);
121+
$this->assertEquals('110 Kingsbrook St.', $model->getOriginal()['address_line_one']);
122+
$this->assertEquals('117 Spencer St.', $model->address->lineOne);
123+
124+
125+
110126
$model = new TestEloquentModelWithCustomCast([
111127
'address' => new Address('110 Kingsbrook St.', 'My Childhood House')
112128
]);

0 commit comments

Comments
 (0)