Laravel-medialibrary: clearMediaCollection without collectionname not deleting all media

Created on 5 May 2018  路  6Comments  路  Source: spatie/laravel-medialibrary

In the documentation it says that with the clearMediaCollection method all media will be deleted.
But this is not the case because the method clearMediaCollection has a default value of 'default' and the filter in the loadMedia method has a check of:

            ->filter(function (Media $mediaItem) use ($collectionName) {
                if ($collectionName == '') {
                    return true;
                }

                return $mediaItem->collection_name === $collectionName;
            })

I think this should be:

            ->filter(function (Media $mediaItem) use ($collectionName) {
                if ($collectionName == 'default') {
                    return true;
                }

                return $mediaItem->collection_name === $collectionName;
            })
bug

All 6 comments

Replacing '' by 'default' in loadMedia doesn't seem to be the solution. 'default' actually is a valid collection name. So it's not that 'default' means "all collections". I'm looking further into this to find a proper solution.

To summarise: @dfoxxdfoxx expects all media to be cleared when calling ->clearMediaCollection(), which is the behaviour described in the docs.

Because the collection name defaults to 'default' when calling clearMediaCollection, not all media items are cleared, but only those in the default collection.

I can see a few ways to fix this:

  • Allow for null to be passed in clearMediaCollection and use null as the default.
  • Change the default from 'default' to '', which will result in all media items to be cleared.

Both solutions are a breaking change.

We could update the docs and say that you should manually pass '' to clear all media items, and tag this issue to revisit for a later version.

@freekmurze what's your opinion?

This is what the docs say:

A collection can have any name you want. If you don't specify a name, the file will be added to a collection named default. You can clear out a specific collection by passing the name to clearMediaCollection

The spirit of the medialib is that if you don't specify a collection name explicitly, we'll use the default collection.

clearMediaCollection() should only remove items from the default collection.

This is also what @dfoxxdfoxx seems to be proposing.

So make sure that clearMediaCollection() only removes stuff from the default collection. I consider this a bugfix.

Hi @davidianbonner @dfoxxdfoxx

Freek and I discussed this in the office today. There was some confusion about what you exactly wanted to do.

The clearMediaCollection method is used to clear one collection, and defaults to default.

If you want to remove all media of a model, regardless of a collection, you can simply use eloquent: $model->media->each->delete().

@dfoxxdfoxx I belive this was meant for you...

Woops.. my bad!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

swash13 picture swash13  路  3Comments

kickthemooon picture kickthemooon  路  4Comments

netanelwebninja picture netanelwebninja  路  3Comments

Nks picture Nks  路  3Comments

intrepidws picture intrepidws  路  3Comments