diff --git a/src/Services/TagService.php b/src/Services/TagService.php index b96a7e0..91bf6e2 100644 --- a/src/Services/TagService.php +++ b/src/Services/TagService.php @@ -354,7 +354,7 @@ public function renameTags(string $oldName, string $newName, $class = null): int return $pivot ->where($relatedKeyName, $oldTag->getKey()) - ->where($relatedMorphType, get_class($class)) + ->where($relatedMorphType, $class->getMorphClass()) ->update([ $relatedKeyName => $newTag->getKey(), ]); diff --git a/tests/TagServiceTests.php b/tests/TagServiceTests.php index 9dbdd30..50fa2cd 100644 --- a/tests/TagServiceTests.php +++ b/tests/TagServiceTests.php @@ -313,6 +313,32 @@ public function testRenamingTag(): void ); } + /** + * Test renaming a tag with a custom morph class key. + */ + public function testRenamingTagWithCustomMorphClass(): void + { + // Create a model with a custom morph class key + $morphModel = new class extends TestModel { + protected $attributes = [ + 'title' => 'testing morph model' + ]; + public function getMorphClass() + { + return 'test-morph-model'; // can any custom key that is different from the class name + } + }; + $morphModel->save(); + + $morphModel->tag('Apple'); + $morphModel->tag('Banana'); + $morphModel->tag('Cherry'); + + // Rename the tags the morphModel class should have exactly 1 update + $count = $this->service->renameTags('Apple', 'Apricot', $morphModel); + $this->assertEquals(1, $count); + } + /** * Test renaming a tag across all models. */