From 5068c75530dcfba48f7a9afb7b3b510b10f101fa Mon Sep 17 00:00:00 2001 From: Alexander Nikushkin Date: Sat, 23 Dec 2023 10:24:35 +0500 Subject: [PATCH] feat: medias sorting Added the ability to sort items --- src/Fields/MediaLibrary.php | 9 +++++++++ 1 file changed, 9 insertions(+) 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); + } }