Skip to content

Commit 9761bc9

Browse files
authored
improve numeric comparison for custom casts (#49504)
1 parent 743c21f commit 9761bc9

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2101,7 +2101,7 @@ public function originalIsEquivalent($key)
21012101
}
21022102

21032103
return is_numeric($attribute) && is_numeric($original)
2104-
&& strcmp((string) $attribute, (string) $original) === 0;
2104+
&& BigDecimal::of($attribute)->isEqualTo($original);
21052105
}
21062106

21072107
/**

tests/Integration/Database/DatabaseEloquentModelCustomCastingTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,33 @@ public function testDeviableCasts()
169169
$this->assertSame((new Decimal('320.988'))->getValue(), $model->price->getValue());
170170
}
171171

172+
public function testDirtyOnCustomNumericCasts()
173+
{
174+
$model = new TestEloquentModelWithCustomCast;
175+
$model->price = '123.00';
176+
$model->save();
177+
178+
$this->assertFalse($model->isDirty());
179+
180+
$model->price = '123.00';
181+
$this->assertFalse($model->isDirty('price'));
182+
183+
$model->price = '123.0';
184+
$this->assertFalse($model->isDirty('price'));
185+
186+
$model->price = '123';
187+
$this->assertFalse($model->isDirty('price'));
188+
189+
$model->price = '00123.00';
190+
$this->assertFalse($model->isDirty('price'));
191+
192+
$model->price = '123.4000';
193+
$this->assertTrue($model->isDirty('price'));
194+
195+
$model->price = '123.0004';
196+
$this->assertTrue($model->isDirty('price'));
197+
}
198+
172199
public function testSerializableCasts()
173200
{
174201
$model = new TestEloquentModelWithCustomCast;

0 commit comments

Comments
 (0)