diff --git a/src/Illuminate/Database/Eloquent/Relations/Concerns/AsPivot.php b/src/Illuminate/Database/Eloquent/Relations/Concerns/AsPivot.php index ac2411f806c6..afd328a7ea94 100644 --- a/src/Illuminate/Database/Eloquent/Relations/Concerns/AsPivot.php +++ b/src/Illuminate/Database/Eloquent/Relations/Concerns/AsPivot.php @@ -14,6 +14,13 @@ trait AsPivot */ public $pivotParent; + /** + * The related model of the relationship. + * + * @var \Illuminate\Database\Eloquent\Model + */ + public $pivotRelated; + /** * The name of the foreign key column. * @@ -214,6 +221,19 @@ public function setPivotKeys($foreignKey, $relatedKey) return $this; } + /** + * Set the related model of the relationship. + * + * @param \Illuminate\Database\Eloquent\Model|null $related + * @return $this + */ + public function setRelatedModel(?Model $related = null) + { + $this->pivotRelated = $related; + + return $this; + } + /** * Determine if the pivot model or given attributes has timestamp attributes. * @@ -326,6 +346,7 @@ protected function newQueryForCollectionRestoration(array $ids) public function unsetRelations() { $this->pivotParent = null; + $this->pivotRelated = null; $this->relations = []; return $this; diff --git a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php index b249858a65be..18a2501b66ef 100644 --- a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php +++ b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php @@ -502,7 +502,9 @@ protected function getCurrentlyAttachedPivots() $pivot = $class::fromRawAttributes($this->parent, (array) $record, $this->getTable(), true); - return $pivot->setPivotKeys($this->foreignPivotKey, $this->relatedPivotKey); + return $pivot + ->setPivotKeys($this->foreignPivotKey, $this->relatedPivotKey) + ->setRelatedModel($this->related); }); } @@ -521,7 +523,9 @@ public function newPivot(array $attributes = [], $exists = false) $this->parent, $attributes, $this->table, $exists, $this->using ); - return $pivot->setPivotKeys($this->foreignPivotKey, $this->relatedPivotKey); + return $pivot + ->setPivotKeys($this->foreignPivotKey, $this->relatedPivotKey) + ->setRelatedModel($this->related); } /** diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php b/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php index 9f9b46e51b95..86e51d2af159 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php @@ -160,6 +160,7 @@ public function newPivot(array $attributes = [], $exists = false) : MorphPivot::fromAttributes($this->parent, $attributes, $this->table, $exists); $pivot->setPivotKeys($this->foreignPivotKey, $this->relatedPivotKey) + ->setRelatedModel($this->related) ->setMorphType($this->morphType) ->setMorphClass($this->morphClass); diff --git a/tests/Database/DatabaseEloquentPivotTest.php b/tests/Database/DatabaseEloquentPivotTest.php index ad774d7c1a68..704a917f7b52 100755 --- a/tests/Database/DatabaseEloquentPivotTest.php +++ b/tests/Database/DatabaseEloquentPivotTest.php @@ -35,6 +35,7 @@ public function testPropertiesAreSetCorrectly() $this->assertSame('connection', $pivot->getConnectionName()); $this->assertSame('table', $pivot->getTable()); $this->assertTrue($pivot->exists); + $this->assertSame($parent, $pivot->pivotParent); } public function testMutatorsAreCalledFromConstructor()