Skip to content

Commit

Permalink
[11.x] Add the pivot's related model when creating from attributes (#…
Browse files Browse the repository at this point in the history
…53694)

* Add the pivot's related model when creating from attributes

* formatting

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
alexwass-lr and taylorotwell authored Dec 2, 2024
1 parent 75a49a1 commit e9dc1c4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
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

0 comments on commit e9dc1c4

Please sign in to comment.