Skip to content

Commit

Permalink
fix eloquent increment or decrement update attributes
Browse files Browse the repository at this point in the history
when execute `$model->increment($column, $amount, $extra)`
the corresponding attributes should updated
  • Loading branch information
xcaptain committed Apr 9, 2017
1 parent 73c673e commit ac2d8fc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ protected function incrementOrDecrement($column, $amount, $extra, $method)
return $query->{$method}($column, $amount, $extra);
}

$this->incrementOrDecrementAttributeValue($column, $amount, $method);
$this->incrementOrDecrementAttributeValue($column, $amount, $method, $extra);

return $query->where(
$this->getKeyName(), $this->getKey()
Expand All @@ -425,9 +425,10 @@ protected function incrementOrDecrement($column, $amount, $extra, $method)
* @param string $method
* @return void
*/
protected function incrementOrDecrementAttributeValue($column, $amount, $method)
protected function incrementOrDecrementAttributeValue($column, $amount, $method, $extra = [])
{
$this->{$column} = $this->{$column} + ($method == 'increment' ? $amount : $amount * -1);
$this->fill($extra);

$this->syncOriginalAttribute($column);
}
Expand Down
13 changes: 8 additions & 5 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1314,10 +1314,13 @@ public function testIncrementOnExistingModelCallsQueryAndSetsAttribute()
$query->shouldReceive('where')->andReturn($query);
$query->shouldReceive('increment');

$model->publicIncrement('foo');

$this->assertEquals(3, $model->foo);
$model->publicIncrement('foo', 1);
$this->assertFalse($model->isDirty());

$model->publicIncrement('foo', 1, ['category' => 1]);
$this->assertEquals(4, $model->foo);
$this->assertEquals(1, $model->category);
$this->assertTrue($model->isDirty('category'));
}

public function testRelationshipTouchOwnersIsPropagated()
Expand Down Expand Up @@ -1651,9 +1654,9 @@ public function setPasswordAttribute($value)
$this->attributes['password_hash'] = sha1($value);
}

public function publicIncrement($column, $amount = 1)
public function publicIncrement($column, $amount = 1, $extra = [])
{
return $this->increment($column, $amount);
return $this->increment($column, $amount, $extra);
}

public function belongsToStub()
Expand Down

0 comments on commit ac2d8fc

Please sign in to comment.