Skip to content

Commit 03e13ca

Browse files
authored
[11.x] Increment/decrement with cast value (#47450)
* Increment/decrement with cast value * Fix test * wip * wip * wip
1 parent d298df7 commit 03e13ca

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,10 @@ protected function incrementOrDecrement($column, $amount, $extra, $method)
967967
return false;
968968
}
969969

970+
if ($this->isClassDeviable($column)) {
971+
$amount = (clone $this)->setAttribute($column, $amount)->getAttributeFromArray($column);
972+
}
973+
970974
return tap($this->setKeysForSaveQuery($query)->{$method}($column, $amount, $extra), function () use ($column) {
971975
$this->syncChanges();
972976

tests/Integration/Database/DatabaseEloquentModelCustomCastingTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ public function testDeviableCasts()
167167
$model->decrement('price', '333.333');
168168

169169
$this->assertSame((new Decimal('320.988'))->getValue(), $model->price->getValue());
170+
171+
$model->increment('price', new Decimal('100.001'));
172+
173+
$this->assertSame((new Decimal('420.989'))->getValue(), $model->price->getValue());
174+
175+
$model->decrement('price', new Decimal('200.002'));
176+
177+
$this->assertSame((new Decimal('220.987'))->getValue(), $model->price->getValue());
170178
}
171179

172180
public function testSerializableCasts()
@@ -427,12 +435,12 @@ public function set($model, $key, $value, $attributes)
427435

428436
public function increment($model, $key, $value, $attributes)
429437
{
430-
return new Decimal($attributes[$key] + $value);
438+
return new Decimal($attributes[$key] + ($value instanceof Decimal ? (string) $value : $value));
431439
}
432440

433441
public function decrement($model, $key, $value, $attributes)
434442
{
435-
return new Decimal($attributes[$key] - $value);
443+
return new Decimal($attributes[$key] - ($value instanceof Decimal ? (string) $value : $value));
436444
}
437445

438446
public function serialize($model, $key, $value, $attributes)

tests/Integration/Database/EloquentModelCustomCastingTest.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function testModelWithCustomCastsWorkWithCustomIncrementDecrement()
181181
$this->assertInstanceOf(Euro::class, $model->amount);
182182
$this->assertEquals('2', $model->amount->value);
183183

184-
$model->incrementAmount(new Euro('1'));
184+
$model->increment('amount', new Euro('1'));
185185
$this->assertEquals('3.00', $model->amount->value);
186186
}
187187

@@ -394,19 +394,19 @@ public function get($model, $key, $value, $attributes)
394394

395395
public function set($model, $key, $value, $attributes)
396396
{
397-
return $value->value;
397+
return $value instanceof Euro ? $value->value : $value;
398398
}
399399

400-
public function increment($model, $key, string $value, $attributes)
400+
public function increment($model, $key, $value, $attributes)
401401
{
402-
$model->$key = new Euro((string) BigNumber::of($model->$key->value)->plus($value)->toScale(2));
402+
$model->$key = new Euro((string) BigNumber::of($model->$key->value)->plus($value->value)->toScale(2));
403403

404404
return $model->$key;
405405
}
406406

407-
public function decrement($model, $key, string $value, $attributes)
407+
public function decrement($model, $key, $value, $attributes)
408408
{
409-
$model->$key = new Euro((string) BigNumber::of($model->$key->value)->subtract($value)->toScale(2));
409+
$model->$key = new Euro((string) BigNumber::of($model->$key->value)->subtract($value->value)->toScale(2));
410410

411411
return $model->$key;
412412
}
@@ -418,9 +418,4 @@ class Member extends Model
418418
protected $casts = [
419419
'amount' => Euro::class,
420420
];
421-
422-
public function incrementAmount(Euro $amount)
423-
{
424-
$this->increment('amount', $amount->value);
425-
}
426421
}

0 commit comments

Comments
 (0)