Skip to content

Commit

Permalink
[10.x] Stop adding constraints twice on *Many to *One relationships v…
Browse files Browse the repository at this point in the history
…ia 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
  • Loading branch information
cosmastech committed Mar 25, 2023
1 parent 6011968 commit 034d849
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ 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,
$this->getFirstKeyName(),
$this->secondKey,
$this->getLocalKeyName(),
$this->getSecondLocalKeyName(),
);
));
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/MorphMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
));
}

/**
Expand Down

0 comments on commit 034d849

Please sign in to comment.