Skip to content

Commit 71034a4

Browse files
[9.x] Add ability to discard Eloquent Model changes (#43772)
* [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]>
1 parent 34a0632 commit 71034a4

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,18 @@ public function isClean($attributes = null)
19021902
return ! $this->isDirty(...func_get_args());
19031903
}
19041904

1905+
/**
1906+
* Discard attribute changes and reset the attributes to their original state.
1907+
*
1908+
* @return $this
1909+
*/
1910+
public function discardChanges()
1911+
{
1912+
[$this->attributes, $this->changes] = [$this->original, []];
1913+
1914+
return $this;
1915+
}
1916+
19051917
/**
19061918
* Determine if the model or any of the given attribute(s) were changed when the model was last saved.
19071919
*

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,6 +2423,23 @@ public function testUnsavedModel()
24232423

24242424
$this->assertNull($user->name);
24252425
}
2426+
2427+
public function testDiscardChanges()
2428+
{
2429+
$user = new EloquentModelStub([
2430+
'name' => 'Taylor Otwell',
2431+
]);
2432+
2433+
$this->assertNotEmpty($user->isDirty());
2434+
$this->assertNull($user->getOriginal('name'));
2435+
$this->assertSame('Taylor Otwell', $user->getAttribute('name'));
2436+
2437+
$user->discardChanges();
2438+
2439+
$this->assertEmpty($user->isDirty());
2440+
$this->assertNull($user->getOriginal('name'));
2441+
$this->assertNull($user->getAttribute('name'));
2442+
}
24262443
}
24272444

24282445
class EloquentTestObserverStub

tests/Integration/Database/EloquentModelTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,36 @@ public function testAttributeChanges()
6464
$this->assertTrue($user->wasChanged());
6565
$this->assertTrue($user->wasChanged('name'));
6666
}
67+
68+
public function testDiscardChanges()
69+
{
70+
$user = TestModel2::create([
71+
'name' => $originalName = Str::random(), 'title' => Str::random(),
72+
]);
73+
74+
$this->assertEmpty($user->getDirty());
75+
$this->assertEmpty($user->getChanges());
76+
$this->assertFalse($user->isDirty());
77+
$this->assertFalse($user->wasChanged());
78+
79+
$user->name = $overideName = Str::random();
80+
81+
$this->assertEquals(['name' => $overideName], $user->getDirty());
82+
$this->assertEmpty($user->getChanges());
83+
$this->assertTrue($user->isDirty());
84+
$this->assertFalse($user->wasChanged());
85+
$this->assertSame($originalName, $user->getOriginal('name'));
86+
$this->assertSame($overideName, $user->getAttribute('name'));
87+
88+
$user->discardChanges();
89+
90+
$this->assertEmpty($user->getDirty());
91+
$this->assertSame($originalName, $user->getOriginal('name'));
92+
$this->assertSame($originalName, $user->getAttribute('name'));
93+
94+
$user->save();
95+
$this->assertFalse($user->wasChanged());
96+
}
6797
}
6898

6999
class TestModel1 extends Model

0 commit comments

Comments
 (0)