Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Stop adding constraints twice on *Many to *One relationships via one() #46575

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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