Skip to content
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
21 changes: 21 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/Concerns/AsPivot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -326,6 +346,7 @@ protected function newQueryForCollectionRestoration(array $ids)
public function unsetRelations()
{
$this->pivotParent = null;
$this->pivotRelated = null;
$this->relations = [];

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand All @@ -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);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Database/Eloquent/Relations/MorphToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions tests/Database/DatabaseEloquentPivotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down