Skip to content

Commit dbfd1c7

Browse files
committed
[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
1 parent 6c07bca commit dbfd1c7

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/Illuminate/Database/Eloquent/Relations/BelongsTo.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ public function match(array $models, Collection $results, $relation)
175175

176176
return $models;
177177
}
178+
179+
/**
180+
* Touch all of the related models for the relationship.
181+
*
182+
* @return void
183+
*/
184+
public function touch()
185+
{
186+
if (! is_null($this->getParentKey())) {
187+
parent::touch();
188+
}
189+
}
178190

179191
/**
180192
* Associate the model instance to the given parent.

src/Illuminate/Database/Eloquent/Relations/MorphTo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public function dissociate()
263263
*/
264264
public function touch()
265265
{
266-
if (! is_null($this->child->{$this->foreignKey})) {
266+
if (! is_null($this->getParentKey())) {
267267
parent::touch();
268268
}
269269
}

0 commit comments

Comments
 (0)