Skip to content

Commit

Permalink
[9.x] Add ability to discard Eloquent Model changes (#43772)
Browse files Browse the repository at this point in the history
* [9.x] Add ability to discard Eloquent Model changes.

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* one line

Signed-off-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
crynobone and taylorotwell authored Sep 8, 2022
1 parent 34a0632 commit 71034a4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1902,6 +1902,18 @@ public function isClean($attributes = null)
return ! $this->isDirty(...func_get_args());
}

/**
* Discard attribute changes and reset the attributes to their original state.
*
* @return $this
*/
public function discardChanges()
{
[$this->attributes, $this->changes] = [$this->original, []];

return $this;
}

/**
* Determine if the model or any of the given attribute(s) were changed when the model was last saved.
*
Expand Down
17 changes: 17 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2423,6 +2423,23 @@ public function testUnsavedModel()

$this->assertNull($user->name);
}

public function testDiscardChanges()
{
$user = new EloquentModelStub([
'name' => 'Taylor Otwell',
]);

$this->assertNotEmpty($user->isDirty());
$this->assertNull($user->getOriginal('name'));
$this->assertSame('Taylor Otwell', $user->getAttribute('name'));

$user->discardChanges();

$this->assertEmpty($user->isDirty());
$this->assertNull($user->getOriginal('name'));
$this->assertNull($user->getAttribute('name'));
}
}

class EloquentTestObserverStub
Expand Down
30 changes: 30 additions & 0 deletions tests/Integration/Database/EloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,36 @@ public function testAttributeChanges()
$this->assertTrue($user->wasChanged());
$this->assertTrue($user->wasChanged('name'));
}

public function testDiscardChanges()
{
$user = TestModel2::create([
'name' => $originalName = Str::random(), 'title' => Str::random(),
]);

$this->assertEmpty($user->getDirty());
$this->assertEmpty($user->getChanges());
$this->assertFalse($user->isDirty());
$this->assertFalse($user->wasChanged());

$user->name = $overideName = Str::random();

$this->assertEquals(['name' => $overideName], $user->getDirty());
$this->assertEmpty($user->getChanges());
$this->assertTrue($user->isDirty());
$this->assertFalse($user->wasChanged());
$this->assertSame($originalName, $user->getOriginal('name'));
$this->assertSame($overideName, $user->getAttribute('name'));

$user->discardChanges();

$this->assertEmpty($user->getDirty());
$this->assertSame($originalName, $user->getOriginal('name'));
$this->assertSame($originalName, $user->getAttribute('name'));

$user->save();
$this->assertFalse($user->wasChanged());
}
}

class TestModel1 extends Model
Expand Down

0 comments on commit 71034a4

Please sign in to comment.