Skip to content

Commit 45c010c

Browse files
committed
Fix cache invalidation when using ->update()
1 parent 749c7ac commit 45c010c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/CachedBuilder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ public function sum($column)
144144
return $this->cachedValue(func_get_args(), $cacheKey);
145145
}
146146

147+
public function update(array $values)
148+
{
149+
$this->checkCooldownAndFlushAfterPersiting($this->model);
150+
151+
return parent::update($values);
152+
}
153+
147154
public function value($column)
148155
{
149156
if (! $this->isCachable()) {

tests/Integration/CachedBuilderTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,4 +909,24 @@ public function testInsertInvalidatesCache()
909909
$this->assertCount(11, $authorsAfterInsert);
910910
$this->assertCount(11, $uncachedAuthors);
911911
}
912+
913+
public function testUpdateInvalidatesCache()
914+
{
915+
$originalAuthor = (new Author)
916+
->first();
917+
$author = (new Author)
918+
->first();
919+
920+
$author->update([
921+
"name" => "Updated Name",
922+
]);
923+
$authorAfterUpdate = (new Author)
924+
->find($author->id);
925+
$uncachedAuthor = (new UncachedAuthor)
926+
->find($author->id);
927+
928+
$this->assertNotEquals($originalAuthor->name, $authorAfterUpdate->name);
929+
$this->assertEquals("Updated Name", $authorAfterUpdate->name);
930+
$this->assertEquals($authorAfterUpdate->name, $uncachedAuthor->name);
931+
}
912932
}

0 commit comments

Comments
 (0)