From 034d849a65fe55bf46b56851f65bd4f5835d02de Mon Sep 17 00:00:00 2001 From: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Sat, 25 Mar 2023 07:54:24 -0400 Subject: [PATCH] [10.x] Stop adding constraints twice on *Many to *One relationships via one() (#46575) * create new HasOne from HasMany without constraints * create MorphMany without constraints * create one from HasManyThrough without duplicating constraints * run noConstraint against MorphOne instead of MorphMany --- src/Illuminate/Database/Eloquent/Relations/HasMany.php | 7 ++++++- .../Database/Eloquent/Relations/HasManyThrough.php | 4 ++-- src/Illuminate/Database/Eloquent/Relations/MorphMany.php | 8 +++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/HasMany.php b/src/Illuminate/Database/Eloquent/Relations/HasMany.php index 9587cac4dd97..27bcd73e39b2 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasMany.php @@ -13,7 +13,12 @@ class HasMany extends HasOneOrMany */ public function one() { - return new HasOne($this->getQuery(), $this->parent, $this->foreignKey, $this->localKey); + return HasOne::noConstraints(fn () => new HasOne( + $this->getQuery(), + $this->parent, + $this->foreignKey, + $this->localKey + )); } /** diff --git a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php index 645581464628..1514274ce380 100644 --- a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php @@ -88,7 +88,7 @@ public function __construct(Builder $query, Model $farParent, Model $throughPare */ public function one() { - return new HasOneThrough( + return HasOneThrough::noConstraints(fn () => new HasOneThrough( $this->getQuery(), $this->farParent, $this->throughParent, @@ -96,7 +96,7 @@ public function one() $this->secondKey, $this->getLocalKeyName(), $this->getSecondLocalKeyName(), - ); + )); } /** diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphMany.php b/src/Illuminate/Database/Eloquent/Relations/MorphMany.php index 6c0fd140fa07..ada088930855 100755 --- a/src/Illuminate/Database/Eloquent/Relations/MorphMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphMany.php @@ -13,7 +13,13 @@ class MorphMany extends MorphOneOrMany */ public function one() { - return new MorphOne($this->getQuery(), $this->getParent(), $this->morphType, $this->foreignKey, $this->localKey); + return MorphOne::noConstraints(fn () => new MorphOne( + $this->getQuery(), + $this->getParent(), + $this->morphType, + $this->foreignKey, + $this->localKey + )); } /**