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

[11.x] Move $ownerKey check for null to MorphTo as BelongsTo relationship will always return a string #53592

Merged
merged 2 commits into from
Nov 20, 2024
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
4 changes: 0 additions & 4 deletions src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,6 @@ public function getOwnerKeyName()
*/
public function getQualifiedOwnerKeyName()
{
if (is_null($this->ownerKey)) {
return '';
}

return $this->related->qualifyColumn($this->ownerKey);
}

Expand Down
23 changes: 18 additions & 5 deletions src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function __construct(Builder $query, Model $parent, $foreignKey, $ownerKe
}

/** @inheritDoc */
#[\Override]
public function addEagerConstraints(array $models)
{
$this->buildDictionary($this->models = Collection::make($models));
Expand Down Expand Up @@ -199,6 +200,7 @@ public function createModelByType($type)
}

/** @inheritDoc */
#[\Override]
public function match(array $models, Collection $results, $relation)
{
return $models;
Expand Down Expand Up @@ -230,6 +232,7 @@ protected function matchToMorphParents($type, Collection $results)
* @param TRelatedModel|null $model
* @return TDeclaringModel
*/
#[\Override]
public function associate($model)
{
if ($model instanceof Model) {
Expand All @@ -254,6 +257,7 @@ public function associate($model)
*
* @return TDeclaringModel
*/
#[\Override]
public function dissociate()
{
$this->parent->setAttribute($this->foreignKey, null);
Expand All @@ -263,11 +267,8 @@ public function dissociate()
return $this->parent->setRelation($this->relationName, null);
}

/**
* Touch all of the related models for the relationship.
*
* @return void
*/
/** @inheritDoc */
#[\Override]
public function touch()
{
if (! is_null($this->getParentKey())) {
Expand All @@ -276,6 +277,7 @@ public function touch()
}

/** @inheritDoc */
#[\Override]
protected function newRelatedInstanceFor(Model $parent)
{
return $parent->{$this->getRelationName()}()->getRelated()->newInstance();
Expand Down Expand Up @@ -412,6 +414,17 @@ protected function replayMacros(Builder $query)
return $query;
}

/** @inheritDoc */
#[\Override]
public function getQualifiedOwnerKeyName()
{
if (is_null($this->ownerKey)) {
return '';
}

return parent::getQualifiedOwnerKeyName();
}

/**
* Handle dynamic method calls to the relationship.
*
Expand Down