Skip to content

Commit

Permalink
[11.x] Move $ownerKey check for null to MorphTo as BelongsTo
Browse files Browse the repository at this point in the history
…relationship will always return a `string` (#53592)

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

Improvements based on #53573 PR

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* formatting

---------

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
crynobone and taylorotwell authored Nov 20, 2024
1 parent 7187a93 commit aa686b6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
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

0 comments on commit aa686b6

Please sign in to comment.