Skip to content
Open
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
44 changes: 40 additions & 4 deletions src/Database/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,62 @@ public function setSimpleValue($value): void
return;
}

// Convert models to keys
// if pivot data doesn't exists, Convert models to keys , else convert models to nested arrays with the keys as the indexes (pivot of relation needs to use Winter\Storm\Database\Pivot)
if ($value instanceof Model) {
$value = $value->getKey();
if ($value->{$this->getPivotAccessor()} instanceof Pivot) {
$value = [$value->getKey() => $value->{$this->getPivotAccessor()}->toArray()];
} else {
$value = $value->getKey();
}
} elseif (is_array($value)) {
$newValue = [];
foreach ($value as $_key => $_value) {
if ($_value instanceof Model) {
$value[$_key] = $_value->getKey();
if ($_value->{$this->getPivotAccessor()} instanceof Pivot) {
$newValue[$_value->getKey()] = $_value->{$this->getPivotAccessor()}->toArray();
} else {
$newValue[] = $_value->getKey();
}
}
}
if (count($newValue) > 0) {
$value = $newValue;
}
}

// Convert scalar to array
if (!is_array($value) && !$value instanceof Collection) {
$value = [$value];
}

//checks if $value has nested arrays(pivot data) and regenerates the keys in a basic array format
$keys = [];
foreach ($value as $_key => $_value) {
if (is_array($_value)) {
$keys[] = $_key;
}
}
if (count($keys) < 1) {
$keys = $value;
$hasPivot = false;
} else {
$hasPivot = true;
}

// Setting the relationship
$relationCollection = $value instanceof Collection
? $value
: $relationModel->whereIn($relationModel->getKeyName(), $value)->get();
: $relationModel->whereIn($relationModel->getKeyName(), $keys)->get();


// If avaliable, associate the pivot relation(s) in memory immediately (pivot of relation needs to use Winter\Storm\Database\Pivot)
if ($hasPivot) {
foreach ($relationCollection as $_relationModel) {
if (isset($value[$_relationModel->getKey()])) {
$_relationModel->setRelation($this->getPivotAccessor(), $this->newPivot($value[$_relationModel->getKey()]));
}
}
}

// Associate in memory immediately
$this->parent->setRelation($this->relationName, $relationCollection);
Expand Down