Skip to content

Commit

Permalink
[11.x] Don't touch BelongsTo relationship when it doesn't exist (lara…
Browse files Browse the repository at this point in the history
…vel#52407)

* [11.x] Don't touch BelongsTo relationship when it doesn't exist

Currently, MorphTo and BelongsToMany check for relation existence before sending the update query to the database.

This adds that check to the BelongsTo relationship.

`getParentKey()` can be used to find the child foreign key value

* Update BelongsTo.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
patrickomeara and taylorotwell authored Aug 7, 2024
1 parent fd0f381 commit dd649c6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ public function disassociate()
return $this->dissociate();
}

/**
* Touch all of the related models for the relationship.
*
* @return void
*/
public function touch()
{
if (! is_null($this->getParentKey())) {
parent::touch();
}
}

/** @inheritDoc */
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
{
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public function dissociate()
*/
public function touch()
{
if (! is_null($this->child->{$this->foreignKey})) {
if (! is_null($this->getParentKey())) {
parent::touch();
}
}
Expand Down

0 comments on commit dd649c6

Please sign in to comment.