Skip to content

Commit 4fbd777

Browse files
author
Pantea Marius-ciclistu
committed
POC for laravel/framework#31778 bulletproofing incrementOrDecrement
1 parent 1a5b70e commit 4fbd777

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/Models/BaseModel.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,13 +469,31 @@ public function getCasts()
469469
*/
470470
protected function incrementOrDecrement($column, $amount, $extra, $method): int
471471
{
472-
$return = (int)parent::incrementOrDecrement($column, $amount, $extra, $method);
472+
if (!$this->exists) {
473+
return $this->newQueryWithoutRelationships()->{$method}($column, $amount, $extra);
474+
}
473475

474-
if ($this->exists) {
475-
$this->incrementsToRefresh[$column] = true;
476+
$this->{$column} = $this->isClassDeviable($column)
477+
? $this->deviateClassCastableAttribute($method, $column, $amount)
478+
: $this->{$column} + ($method === 'increment' ? $amount : $amount * -1);
479+
480+
$this->forceFill($extra);
481+
482+
if (!$this->isDirty() || $this->fireModelEvent('updating') === false) {
483+
return 0;
476484
}
477485

478-
return $return;
486+
return (int)tap(
487+
$this->setKeysForSaveQuery($this->newQueryWithoutScopes())->{$method}($column, $amount, $extra),
488+
function () use ($column) {
489+
$this->syncChanges();
490+
491+
$this->fireModelEvent('updated', false);
492+
493+
$this->syncOriginalAttributes(\array_keys($this->changes));
494+
$this->incrementsToRefresh[$column] = true;
495+
}
496+
);
479497
}
480498

481499
/**

0 commit comments

Comments
 (0)