diff --git a/src/Fields/MediaLibrary.php b/src/Fields/MediaLibrary.php index 0fc2919..18fe527 100644 --- a/src/Fields/MediaLibrary.php +++ b/src/Fields/MediaLibrary.php @@ -82,6 +82,8 @@ protected function resolveAfterApply(mixed $data): mixed $this->removeOldMedia($data, $recentlyCreated, $oldValues); + $this->orderMedia($recentlyCreated, $oldValues); + return null; } @@ -112,4 +114,11 @@ private function addMedia(HasMedia $item, UploadedFile $file): Media ->preservingOriginal() ->toMediaCollection($this->column()); } + + private function orderMedia(Collection $recentlyCreated, Collection $oldValues): void + { + $ids = $oldValues->merge($recentlyCreated)->pluck('id')->toArray(); + + Media::setNewOrder($ids); + } }