From 1af0c02bfd9cf95ea79312fa567eea3321c0f656 Mon Sep 17 00:00:00 2001 From: Federico Marquesto Date: Wed, 15 May 2024 15:52:00 -0300 Subject: [PATCH] handle when the relation is null. e.g: when we set null to a relation key --- src/Behaviours/CountCache/CountCache.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Behaviours/CountCache/CountCache.php b/src/Behaviours/CountCache/CountCache.php index 672fd56..f71dfef 100644 --- a/src/Behaviours/CountCache/CountCache.php +++ b/src/Behaviours/CountCache/CountCache.php @@ -44,7 +44,11 @@ public function update(): void $originalRelatedModel = $config->emptyRelatedModel($this->model)->find($this->model->getOriginal($foreignKey)); $this->updateCacheValue($originalRelatedModel, $config, -1); - $this->updateCacheValue($config->relatedModel($this->model), $config, 1); + + // if the relation is null, then we don't need to do anything else. + if($this->model->{$foreignKey}) { + $this->updateCacheValue($config->relatedModel($this->model), $config, 1); + } }); }