laravel-medialibrary

Upgrading

Because there are many breaking changes an upgrade is not that easy. There are many edge cases this guide does not cover. We accept PRs to improve this guide.

From v10 to v11

From v9 to v10

Upgrading from v9 to v10 is straightforward. The biggest change is that we dropped support for PHP 7, and are using PHP 8 features.

From v8 to v9

use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; use Spatie\MediaLibrary\MediaCollections\Models\Media;

class AddGeneratedConversionsToMediaTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { if ( ! Schema::hasColumn( ‘media’, ‘generated_conversions’ ) ) { Schema::table( ‘media’, function ( Blueprint $table ) { $table->json( ‘generated_conversions’ )->nullable(); } ); }

    Media::query()
        ->where(function ($query) {
            $query->whereNull('generated_conversions')
                ->orWhere('generated_conversions', '')
                ->orWhereRaw("JSON_TYPE(generated_conversions) = 'NULL'");
        })
        ->whereRaw("JSON_LENGTH(custom_properties) > 0")
        ->update([
            'generated_conversions' => DB::raw("JSON_EXTRACT(custom_properties, '$.generated_conversions')"),
            // OPTIONAL: Remove the generated conversions from the custom_properties field as well:
            // 'custom_properties'     => DB::raw("JSON_REMOVE(custom_properties, '$.generated_conversions')")
        ]);
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down() {
    /* Restore the 'generated_conversions' field in the 'custom_properties' column if you removed them in this migration
    Media::query()
            ->whereRaw("JSON_TYPE(generated_conversions) != 'NULL'")
            ->update([
                'custom_properties' => DB::raw("JSON_SET(custom_properties, '$.generated_conversions', generated_conversions)")
            ]);
    */

    Schema::table( 'media', function ( Blueprint $table ) {
        $table->dropColumn( 'generated_conversions' );
    } );
} } ```

From v7 to v8

You can use this snippet (in e.g. tinker) to fill the uuid field:

use Spatie\MediaLibrary\MediaCollections\Models\Media;
Media::cursor()->each(
   fn (Media $media) => $media->update(['uuid' => Str::uuid()])
);

class RenameResponsiveImagesCollectionNameInMedia extends Migration {

const OLD_COLLECTION_NAME = 'medialibrary_original';
const NEW_COLLECTION_NAME = 'media_library_original';

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    $this->change(self::OLD_COLLECTION_NAME, self::NEW_COLLECTION_NAME);
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    $this->change(self::NEW_COLLECTION_NAME, self::OLD_COLLECTION_NAME);
}

public function change(string $from, string $to)
{
    /** @var Factory $filesystem */
    $filesystem = app(Factory::class);

    $pathGenerator = PathGeneratorFactory::create();

    // Find media with the old collection name is present
    Media::query()
        ->withoutGlobalScopes()
        ->whereNotNull('responsive_images->' . $from)
        ->cursor()
        ->each(function($media) use ($from, $to, $filesystem, $pathGenerator) {
            // Change the old collection key
            $responsive_images = array_merge(
                $media->responsive_images,
                [
                    $to => $media->responsive_images[$from],
                    $from => null
                ]
            );
            // Remove it completely
            unset($responsive_images[$from]);

            // Responsive image path for this media
            $directory = $pathGenerator->getPathForResponsiveImages($media);
            // Media disk
            $disk = $filesystem->disk($media->disk);

            foreach($responsive_images[$to]['urls'] as &$filename) {
                // Replace the old collection name with the new one
                $newFilename = str_replace(
                    $from,
                    $to,
                    $filename
                );
                // If the old file exists move it on disk
                if($disk->exists($directory . $filename)) {
                    $disk->move($directory . $filename, $directory . $newFilename);
                    // Update the new array by ref
                    $filename = $newFilename;
                }
            }
            // Save the new array
            $media->responsive_images = $responsive_images;
            $media->save();
        });
} } ```

7.3.0

7.1.0

From v6 to v7

From v5 to v6

Change every instance of

  public function registerMediaConversions()

to

 public function registerMediaConversions(Media $media = null)

From v4 to v5

From v3 to v4

From v2 to v3

You can upgrade from v2 to v3 by performing these renames in your model that has media.

In the config file you should rename the filesystem-option to default_filesystem.

In the db the temp-column must be removed. Add these columns:

Note that this behaviour has changed:

From v1 to v2

Because v2 is a complete rewrite a simple upgrade path is not available. If you want to upgrade completely remove the v1 package and follow install instructions of v2.