Skip to content
Closed
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
50 changes: 37 additions & 13 deletions src/Database/Concerns/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,22 @@ public function getRelationDefinition($name)
public function getRelationTypeDefinitions($type)
{
if (in_array($type, static::$relationTypes)) {
return $this->{$type};
$definitions = $this->{$type} ?: [];

// Handle renaming otherKey to relatedKey
// @see https://github.com/laravel/framework/commit/d2a77776295cb155b985526c1fa1fddc190adb07
// @since v1.1.8
foreach ($definitions as $relation => $options) {
if (
is_array($options)
&& array_key_exists('otherKey', $options)
&& !array_key_exists('relatedPivotKey', $options)
) {
$definitions[$relation]['relatedPivotKey'] = $options['otherKey'];
}
}

return $definitions;
}

return [];
Expand Down Expand Up @@ -309,18 +324,21 @@ protected function handleRelation($relationName)
switch ($relationType) {
case 'hasOne':
case 'hasMany':
$relation = $this->validateRelationArgs($relationName, ['key', 'otherKey']);
$relationObj = $this->$relationType($relation[0], $relation['key'], $relation['otherKey'], $relationName);
$relation = $this->validateRelationArgs($relationName, ['key', 'relatedKey']);
$relationObj = $this->$relationType($relation[0], $relation['key'], $relation['relatedKey'], $relationName);
break;

case 'belongsTo':
$relation = $this->validateRelationArgs($relationName, ['key', 'otherKey']);
$relationObj = $this->$relationType($relation[0], $relation['key'], $relation['otherKey'], $relationName);
$relation = $this->validateRelationArgs($relationName, ['key', 'relatedKey']);
$relationObj = $this->$relationType($relation[0], $relation['key'], $relation['relatedKey'], $relationName);
break;

case 'belongsToMany':
$relation = $this->validateRelationArgs($relationName, ['table', 'key', 'otherKey', 'parentKey', 'relatedKey', 'pivot', 'timestamps']);
$relationObj = $this->$relationType($relation[0], $relation['table'], $relation['key'], $relation['otherKey'], $relation['parentKey'], $relation['relatedKey'], $relationName);
$relation = $this->validateRelationArgs($relationName, ['table', 'key', 'relatedPivotKey', 'parentKey', 'relatedKey', 'pivot', 'timestamps']);
$relationObj = $this->$relationType($relation[0], $relation['table'], $relation['key'], $relation['relatedPivotKey'], $relation['parentKey'], $relation['relatedKey'], $relationName);
if (!empty($relation['pivotModel'])) {
$relationObj->using($relation['pivotModel']);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be added to the other pivot model using relations (morphToMany & morphedByMany)

break;

case 'morphTo':
Expand All @@ -335,13 +353,19 @@ protected function handleRelation($relationName)
break;

case 'morphToMany':
$relation = $this->validateRelationArgs($relationName, ['table', 'key', 'otherKey', 'parentKey', 'relatedKey', 'pivot', 'timestamps'], ['name']);
$relationObj = $this->$relationType($relation[0], $relation['name'], $relation['table'], $relation['key'], $relation['otherKey'], $relation['parentKey'], $relation['relatedKey'], false, $relationName);
$relation = $this->validateRelationArgs($relationName, ['table', 'key', 'relatedPivotKey', 'parentKey', 'relatedKey', 'pivot', 'timestamps'], ['name']);
$relationObj = $this->$relationType($relation[0], $relation['name'], $relation['table'], $relation['key'], $relation['relatedPivotKey'], $relation['parentKey'], $relation['relatedKey'], false, $relationName);
if (!empty($relation['pivotModel'])) {
$relationObj->using($relation['pivotModel']);
}
break;

case 'morphedByMany':
$relation = $this->validateRelationArgs($relationName, ['table', 'key', 'otherKey', 'parentKey', 'relatedKey', 'pivot', 'timestamps'], ['name']);
$relationObj = $this->$relationType($relation[0], $relation['name'], $relation['table'], $relation['key'], $relation['otherKey'], $relation['parentKey'], $relation['relatedKey'], $relationName);
$relation = $this->validateRelationArgs($relationName, ['table', 'key', 'relatedPivotKey', 'parentKey', 'relatedKey', 'pivot', 'timestamps'], ['name']);
$relationObj = $this->$relationType($relation[0], $relation['name'], $relation['table'], $relation['key'], $relation['relatedPivotKey'], $relation['parentKey'], $relation['relatedKey'], $relationName);
if (!empty($relation['pivotModel'])) {
$relationObj->using($relation['pivotModel']);
}
break;

case 'attachOne':
Expand All @@ -352,8 +376,8 @@ protected function handleRelation($relationName)

case 'hasOneThrough':
case 'hasManyThrough':
$relation = $this->validateRelationArgs($relationName, ['key', 'throughKey', 'otherKey', 'secondOtherKey'], ['through']);
$relationObj = $this->$relationType($relation[0], $relation['through'], $relation['key'], $relation['throughKey'], $relation['otherKey'], $relation['secondOtherKey']);
$relation = $this->validateRelationArgs($relationName, ['key', 'throughKey', 'relatedKey', 'secondOtherKey'], ['through']);
$relationObj = $this->$relationType($relation[0], $relation['through'], $relation['key'], $relation['throughKey'], $relation['relatedKey'], $relation['secondOtherKey']);
break;

default:
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ public function newPivot(EloquentModel $parent, array $attributes, $table, $exis
{
return $using
? $using::fromRawAttributes($parent, $attributes, $table, $exists)
: new Pivot($parent, $attributes, $table, $exists);
: Pivot::fromAttributes($parent, $attributes, $table, $exists);
}

/**
Expand All @@ -741,7 +741,7 @@ public function newRelationPivot($relationName, $parent, $attributes, $table, $e

if (!is_null($definition) && array_key_exists('pivotModel', $definition)) {
$pivotModel = $definition['pivotModel'];
return new $pivotModel($parent, $attributes, $table, $exists);
return $pivotModel::fromAttributes($parent, $attributes, $table, $exists);
}
}

Expand Down
Loading