Skip to content

Commit 723dc28

Browse files
Merge branch '6.x' into 7.x
2 parents 5c841b3 + 90892f9 commit 723dc28

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,8 @@ public function originalIsEquivalent($key)
14111411
} elseif ($this->hasCast($key, ['object', 'collection'])) {
14121412
return $this->castAttribute($key, $attribute) ==
14131413
$this->castAttribute($key, $original);
1414+
} elseif ($this->hasCast($key, ['real', 'float', 'double'])) {
1415+
return abs($this->castAttribute($key, $attribute) - $this->castAttribute($key, $original)) < PHP_FLOAT_EPSILON * 4;
14141416
} elseif ($this->hasCast($key, static::$primitiveCastTypes)) {
14151417
return $this->castAttribute($key, $attribute) ===
14161418
$this->castAttribute($key, $original);

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ public function testCleanAttributes()
159159
$this->assertFalse($model->isClean(['foo', 'bar']));
160160
}
161161

162+
public function testCleanWhenFloatUpdateAttribute()
163+
{
164+
// test is equivalent
165+
$model = new EloquentModelStub(['castedFloat' => 8 - 6.4]);
166+
$model->syncOriginal();
167+
$this->assertTrue($model->originalIsEquivalent('castedFloat', 1.6));
168+
169+
// test is not equivalent
170+
$model = new EloquentModelStub(['castedFloat' => 5.6]);
171+
$model->syncOriginal();
172+
$this->assertFalse($model->originalIsEquivalent('castedFloat', 5.5));
173+
}
174+
162175
public function testCalculatedAttributes()
163176
{
164177
$model = new EloquentModelStub;
@@ -2165,6 +2178,7 @@ class EloquentModelStub extends Model
21652178
protected $table = 'stub';
21662179
protected $guarded = [];
21672180
protected $morph_to_stub_type = EloquentModelSaveStub::class;
2181+
protected $casts = ['castedFloat' => 'float'];
21682182

21692183
public function getListItemsAttribute($value)
21702184
{

0 commit comments

Comments
 (0)