F
Filament13mo ago
Simon

SpatieMediaLibraryFileUpload not working on $this->form->fill() after upgrading to v3

I upgraded to Filament v3 and everything has been super smooth except for one part For some reason the SpatieMediaLibrary plugin (which is also upgraded to v3) is not operating as expected. I'm getting the following error when my form component loads:
Filament\Forms\Components\SpatieMediaLibraryFileUpload::Filament\Forms\Components\{closure}(): Argument #2 ($file) must be of type string, array given, called in /vendor/filament/support/src/Concerns/EvaluatesClosures.php on line 35
Filament\Forms\Components\SpatieMediaLibraryFileUpload::Filament\Forms\Components\{closure}(): Argument #2 ($file) must be of type string, array given, called in /vendor/filament/support/src/Concerns/EvaluatesClosures.php on line 35
This seems to stem from using the media library to populate the form in the mount() method of the component, which looks like this:
public function mount()
{
$this->developer = auth()->user()->developer;

$this->form->fill([
'photo' => $this->developer->getMedia('photo')
]);
}
public function mount()
{
$this->developer = auth()->user()->developer;

$this->form->fill([
'photo' => $this->developer->getMedia('photo')
]);
}
If I remove this line or change it to a simple value (e.g. a string), the form loads and the component renders, but obviously it doesn't load with any pre-filled data (i.e. any images that were previously uploaded) Does anyone know what needs to change here?
Solution:
I found the cause... I was missing the call to ->model() on the end of my $form in the component ``` // Before... public function form(Form $form): Form...
Jump to solution
3 Replies
Lara Zeus
Lara Zeus13mo ago
are you casting it to array in your model? maybe then you need to add ->multiple()!
Simon
SimonOP13mo ago
I've not seen any references to casting this in the model or any referencing to casting in the docs of either the Filament wrapper or the Spatie Media Library package... The way the model registers its interest in managing media library uploads is by implementing the Spatie\MediaLibrary\HasMedia interface and using the Spatie\MediaLibrary\InteractsWithMedia trait My model overrides the registerMediaCollections() method as needed to create the following photo media collection:
public function registerMediaCollections()
{
$this
->addMediaCollection('photo')
->singleFile()
->useFallbackUrl($this->defaultPhotoUrl())
->registerMediaConversions(function (Media $media) {
$this
->addMediaConversion('thumbnail')
->fit(Manipulations::FIT_CROP, 150, 150);

$this
->addMediaConversion('preview')
->fit(Manipulations::FIT_CROP, 300, 300);

$this
->addMediaConversion('big')
->fit(Manipulations::FIT_CROP, 512, 512);
});
}
public function registerMediaCollections()
{
$this
->addMediaCollection('photo')
->singleFile()
->useFallbackUrl($this->defaultPhotoUrl())
->registerMediaConversions(function (Media $media) {
$this
->addMediaConversion('thumbnail')
->fit(Manipulations::FIT_CROP, 150, 150);

$this
->addMediaConversion('preview')
->fit(Manipulations::FIT_CROP, 300, 300);

$this
->addMediaConversion('big')
->fit(Manipulations::FIT_CROP, 512, 512);
});
}
This all worked in Filament v2 and I can't find any reference to anything changing (either that has changed or should change) that would cause this to stop working
Solution
Simon
Simon13mo ago
I found the cause... I was missing the call to ->model() on the end of my $form in the component
// Before...
public function form(Form $form): Form
{
return $form->schema([...])->statePath('data');
}

// After...
public function form(Form $form): Form
{
return $form->schema([...])->statePath('data')->model($modelInstance);
}
// Before...
public function form(Form $form): Form
{
return $form->schema([...])->statePath('data');
}

// After...
public function form(Form $form): Form
{
return $form->schema([...])->statePath('data')->model($modelInstance);
}
Want results from more Discord servers?
Add your server