From 3928fa7eeeb6b4261504a29b3e54cfe2708c172c Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Thu, 30 Mar 2023 17:45:55 +0200 Subject: [PATCH] Improve siblings eager loading query --- src/Eloquent/Relations/Siblings.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Eloquent/Relations/Siblings.php b/src/Eloquent/Relations/Siblings.php index 59fbec1..d2803ca 100644 --- a/src/Eloquent/Relations/Siblings.php +++ b/src/Eloquent/Relations/Siblings.php @@ -67,11 +67,15 @@ public function addEagerConstraints(array $models) { $keys = $this->getKeys($models, $this->localKey); - $this->query->whereIn($this->foreignKey, $keys); + $this->query->where( + function (Builder $query) use ($keys) { + $query->whereIn($this->foreignKey, $keys); - if (in_array(null, $keys, true)) { - $this->query->orWhereNull($this->foreignKey); - } + if (in_array(null, $keys, true)) { + $query->orWhereNull($this->foreignKey); + } + } + ); } /**