filament icon indicating copy to clipboard operation
filament copied to clipboard

Spatie Laravel Media Library Plugin - Error if using Closure on ->collection()

Open monteduro opened this issue 2 years ago • 5 comments

Package

filament/spatie-laravel-media-library-plugin

Package Version

v3.2.36

Laravel Version

v10.47.0

Livewire Version

v3.4.8

PHP Version

8.2.15

Problem description

Starting from version 3.2.36, there is a new code block in the method applyEagerLoading of Filament\Tables\Columns\SpatieMediaLibraryImageColumn that is causing a crash if the collection method is provided by a Closure.

/** @phpstan-ignore-next-line */
$modifyMediaQuery = fn (Builder | Relation $query) => $query
    ->ordered()
    ->when(
        $this->getCollection(), // <- this is the problem
        fn (Builder | Relation $query, string $collection) => $query->where(
            'collection_name',
            $collection,
        ),
    );
SpatieMediaLibraryImageColumn::make('media')
          ->collection( function(Media $record) {
              return $record->media()->first()?->collection_name;
          }),

The error:

AdvancedMediaHandler\Filament\Resources\MediaResource\Pages\ListMedia::AdvancedMediaHandler\Filament\Resources\MediaResource\Pages\{closure}(): Argument #1 ($record) must be of type AdvancedMediaHandler\Models\Media, null given, called in /var/www/html/vendor/filament/support/src/Concerns/EvaluatesClosures.php on line 35

if i go back to te v3.2.35 everything is working fine.

Expected behavior

I expect that a method accepting a Closure does not throw an error.

Steps to reproduce

Using the Reproduction repository

  1. Clone

  2. cp .env.example .env

  3. composer install

  4. php artisan key:generate

  5. php artisan migrate

  6. php artisan db:seed

  7. php artisan serve

  8. go to /admin

  9. Click to user and error appear

  10. Install "filament/spatie-laravel-media-library-plugin" v3.2.36.

  11. Utilize the SpatieMediaLibraryMediaColumn column in a table as shown in the following example:

SpatieMediaLibraryMediaColumn::make('media')
  ->collection( function(ModelsMedia $record) {
      return $record->collection ?? 'images';
  }),

Reproduction repository

https://github.com/monteduro/spatie-laravel-media-library-plugin-bug

Relevant log output

No response

monteduro avatar Mar 09 '24 15:03 monteduro

This was an unfortunate breaking change but it's for the best. It only happens when you have a typed $record arg. We made the change so we aren't eager loading media from collections we do not use.

You need to return null if the $record is null, so all images get eager loaded in.

Alternatively, $record->media()->first()?->collection_name looks like you don't even need collection() at all, it is just getting whatever collection is defined. It's also an N+1 query. Maybe remove it?

danharrin avatar Mar 10 '24 19:03 danharrin

This is exactly what i need. Show in a table all the records with his own media that can be “images” or “documents” (i’ve a fallback image for supported extension) or whatever. Mine is a media library where a model is related to a spatie model. This way i can reference that model anywhere in my project and a single media can be used on multiple other entities.

monteduro avatar Mar 10 '24 22:03 monteduro

Yeah, so you should be able to remove the collection() to load all media?

danharrin avatar Mar 11 '24 08:03 danharrin

If I remove the collection() method, the block of code in applyEagerLoading will default to filtering by 'default,' and then nothing will be returned as media because all of my media elements are either 'images' or 'documents.'

I think that the modifyMediaQuery should only be executed if a collection() is provided; otherwise, it will always default to 'default' and will not display anything as an image.

I understand that we are discussing a SpatieMediaLibraryImageColumn, so it should always be an 'image' or something similar, but I still display icons for other types of documents.

A possible solution could be to use $this->collection instead of $this->getCollection(). This is because the method will return "default" if nothing is passed to the ->collection() method, whereas $this->collection will be empty if nothing is passed, and then the Builder will not add any filter.

/** @phpstan-ignore-next-line */
$modifyMediaQuery = fn (Builder | Relation $query) => $query
    ->ordered()
    ->when(
        $this->collection,
        fn (Builder | Relation $query, string $collection) => $query->where(
            'collection_name',
            $collection,
        ),
    );

Screenshot 2024-03-11 alle 13 04 09

monteduro avatar Mar 11 '24 12:03 monteduro

In #11863 you can add allCollections() to show media from all collections

danharrin avatar Mar 14 '24 23:03 danharrin