src/HasMedia/HasMediaTrait.php
public function updateMedia(array $newMediaArray, string $collectionName = 'default'): Collection
    {
        $this->removeMediaItemsNotPresentInArray($newMediaArray, $collectionName);

        return collect($newMediaArray)
            ->map(function (array $newMediaItem) use ($collectionName) {
                static $orderColumn = 1;

                $mediaClass = config('medialibrary.media_model');
                $currentMedia = $mediaClass::findOrFail($newMediaItem['id']);

                if ($currentMedia->collection_name !== $collectionName) {
                    throw MediaCannotBeUpdated::doesNotBelongToCollection($collectionName, $currentMedia);
                }

                if (array_key_exists('name', $newMediaItem)) {
                    $currentMedia->name = $newMediaItem['name'];
                }

                if (array_key_exists('custom_properties', $newMediaItem)) {
                    $currentMedia->custom_properties = $newMediaItem['custom_properties'];
                }

                $currentMedia->order_column = $orderColumn++;

                $currentMedia->save();

                return $currentMedia;
            });
    }
public function updateMedia(array $newMediaArray, string $collectionName = 'default'): Collection
    {
        $this->removeMediaItemsNotPresentInArray($newMediaArray, $collectionName);

        return collect($newMediaArray)
            ->map(function (array $newMediaItem) use ($collectionName) {
                static $orderColumn = 1;

                $mediaClass = config('medialibrary.media_model');
                $currentMedia = $mediaClass::findOrFail($newMediaItem['id']);

                if ($currentMedia->collection_name !== $collectionName) {
                    throw MediaCannotBeUpdated::doesNotBelongToCollection($collectionName, $currentMedia);
                }

                if (array_key_exists('name', $newMediaItem)) {
                    $currentMedia->name = $newMediaItem['name'];
                }

                if (array_key_exists('custom_properties', $newMediaItem)) {
                    $currentMedia->custom_properties = $newMediaItem['custom_properties'];
                }

                $currentMedia->order_column = $orderColumn++;

                $currentMedia->save();

                return $currentMedia;
            });
    }
public function updateMedia ( array $ newMediaArray , string $ collectionName = 'default' ) : Collection { $ this -> removeMediaItemsNotPresentInArray ( $ newMediaArray , $ collectionName ) ; return collect ( $ newMediaArray ) -> map ( function ( array $ newMediaItem ) use ( $ collectionName ) { static $ orderColumn = 1 ; $ mediaClass = config ( 'medialibrary.media_model' ) ; $ currentMedia = $ mediaClass :: findOrFail ( $ newMediaItem [ 'id' ] ) ; if ( $ currentMedia -> collection_name !== $ collectionName ) { throw MediaCannotBeUpdated :: doesNotBelongToCollection ( $ collectionName , $ currentMedia ) ; } if ( array_key_exists ( 'name' , $ newMediaItem ) ) { $ currentMedia -> name = $ newMediaItem [ 'name' ] ; } if ( array_key_exists ( 'custom_properties' , $ newMediaItem ) ) { $ currentMedia -> custom_properties = $ newMediaItem [ 'custom_properties' ] ; } $ currentMedia -> order_column = $ orderColumn ++ ; $ currentMedia -> save ( ) ; return $ currentMedia ; } ) ; }
Update a media collection by deleting and inserting again with new values.

@param array $newMediaArray
@param string $collectionName

@return \Illuminate\Support\Collection

@throws \Spatie\MediaLibrary\Exceptions\MediaCannotBeUpdated
Update a media collection by deleting and inserting again with new values .