Skip to content

Commit 02bd9cc

Browse files
author
Pantea Marius-ciclistu
committed
POC for laravel/framework#31778 bulletproofing incrementOrDecrement
1 parent a1f2197 commit 02bd9cc

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

illuminate/Database/Eloquent/Model.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,15 +980,19 @@ protected function incrementOrDecrement($column, $amount, $extra, $method)
980980

981981
$this->{$column} = $this->isClassDeviable($column)
982982
? $this->deviateClassCastableAttribute($method, $column, $amount)
983-
: $this->{$column} + ($method === 'increment' ? $amount : $amount * -1);
983+
: (\extension_loaded('bcmath') ? \bcadd(
984+
$s1 = (string)$this->{$column},
985+
$s2 = (string)($method === 'increment' ? $amount : $amount * -1),
986+
\max(\strlen(\strrchr($s1, '.') ?: ''), \strlen(\strrchr($s2, '.') ?: ''))
987+
) : $this->{$column} + ($method === 'increment' ? $amount : $amount * -1));
984988

985989
$this->forceFill($extra);
986990

987991
if (!$this->isDirty() || $this->fireModelEvent('updating') === false) {
988992
return 0;
989993
}
990994

991-
return tap(
995+
return (int)tap(
992996
$this->setKeysForSaveQuery($this->newQueryWithoutScopes())->{$method}($column, $amount, $extra),
993997
function () use ($column) {
994998
$this->syncChanges();

0 commit comments

Comments
 (0)