Skip to content

Commit

Permalink
added BC fix for 5.6~5.8
Browse files Browse the repository at this point in the history
  • Loading branch information
yurii-github committed Oct 28, 2020
1 parent 84cc297 commit 53dc79d
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@ public function getResults()
*/
public function associate($model)
{
if (is_array($this->ownerKey)) {

$ownerKey = $model instanceof Model ? $model->getAttribute($this->ownerKey) : $model;
for ($i = 0; $i < count($this->foreignKey); $i++) {
$foreignKey = $this->foreignKey[$i];
$value = $ownerKey[$i];
$this->child->setAttribute($foreignKey, $value);
}

if ($model instanceof Model) {
$this->child->setRelation($this->relationName, $model);
} else {
$this->child->unsetRelation($this->relationName);
}

return $this->child;

} else {
if (!is_array($this->ownerKey)) {
return parent::associate($model);
}

$ownerKey = $model instanceof Model ? $model->getAttribute($this->ownerKey) : $model;
for ($i = 0; $i < count($this->foreignKey); $i++) {
$foreignKey = $this->foreignKey[$i];
$value = $ownerKey[$i];
$this->child->setAttribute($foreignKey, $value);
}
// BC break in 5.8 : https://github.com/illuminate/database/commit/87b9833019f48b88d98a6afc46f38ce37f08237d
$relationName = property_exists($this, 'relationName') ? $this->relationName : $this->relation;
if ($model instanceof Model) {
$this->child->setRelation($relationName, $model);
// proper unset // https://github.com/illuminate/database/commit/44411c7288fc7b7d4e5680cfcdaa46d348b5c981
} elseif ($this->child->isDirty($this->foreignKey)) {
$this->child->unsetRelation($relationName);
}

return $this->child;
}

/**
Expand Down

0 comments on commit 53dc79d

Please sign in to comment.