Skip to content

Commit

Permalink
Had some Issues When Testing Sorted Them
Browse files Browse the repository at this point in the history
  • Loading branch information
abbasmashaddy72 committed May 18, 2024
1 parent 3a4b9ad commit c1e0ff0
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Components/Forms/CuratorPicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,26 +521,29 @@ public function relationship(string | Closure $relationshipName, string | Closur
$orderColumn = $component->getOrderColumn();
$typeColumn = $component->getTypeColumn();
$typeValue = $component->getTypeValue();
$existingIds = $relationship->pluck('id')->toArray();
$existingItems = $relationship->where($typeColumn, $typeValue)->get()->keyBy('media_id')->toArray();
$newIds = collect($state)->pluck('id')->toArray();

$relationship->whereNotIn('id', Arr::pluck($state, 'id'))->delete();
$relationship->whereNotIn('media_id', $newIds)
->where($typeColumn, $typeValue)
->delete();

$i = 1;
$i = count($existingItems) + 1;
foreach ($state as $item) {
$itemId = $item['id'];
$data = [
'media_id' => $itemId,
$orderColumn => $i,
];
if ($typeColumn && $typeValue) {
if ($typeValue) {
$data[$typeColumn] = $typeValue;
}
if (in_array($itemId, $existingIds)) {
if (isset($existingItems[$itemId])) {
$relationship->where('media_id', $itemId)->update($data);
} else {
$relationship->create($data);
}
$i++; // Increment counter
$i++;
}
return;
}
Expand Down

0 comments on commit c1e0ff0

Please sign in to comment.