From 05f746a638a114d00d475b8585dbda570ef5a0bd Mon Sep 17 00:00:00 2001 From: Roy de Jong Date: Sat, 20 Jan 2024 02:01:44 +0100 Subject: [PATCH] fix: HasMany - hookResults param is ignored --- lib/Relationships/HasManyRelationship.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/Relationships/HasManyRelationship.php b/lib/Relationships/HasManyRelationship.php index e72f4e7..e182da2 100644 --- a/lib/Relationships/HasManyRelationship.php +++ b/lib/Relationships/HasManyRelationship.php @@ -58,10 +58,12 @@ public function query(bool $hookResults = true): ModelQuery $query = $this->referenceModel::query() ->where("{$this->foreignKeyColumn} = ?", $pkVal); - $query->addResultHook(function (Model $model) { - $pkVal = $model->getPrimaryKeyValue(); - $this->loadedModels[$pkVal] = $model; - }); + if ($hookResults) { + $query->addResultHook(function (Model $model) { + $pkVal = $model->getPrimaryKeyValue(); + $this->loadedModels[$pkVal] = $model; + }); + } return $query; }