Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] fix eloquent increment or decrement update attributes #18739

Merged
merged 1 commit into from
Apr 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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