F
Filament10mo ago
tesse05

SpatieMediaLibraryFileUpload not showing

I'm using Form Builder in a Livewire normal component. I have this field:
SpatieMediaLibraryFileUpload::make('main_image')
->disk('media')
->acceptedFileTypes([
'image/png', 'image/jpeg'
])
->enableDownload()
->collection('main-image'),
SpatieMediaLibraryFileUpload::make('main_image')
->disk('media')
->acceptedFileTypes([
'image/png', 'image/jpeg'
])
->enableDownload()
->collection('main-image'),
firstly, when I do submit I save the media like this:
foreach($this->main_image as $image) {
$course->addMedia($image)
->toMediaCollection('main-image','media');
}
foreach($this->main_image as $image) {
$course->addMedia($image)
->toMediaCollection('main-image','media');
}
At that point, media is saved successfully in the database, and attached correctly to the model. When I refresh the page, the Spatie field looks like empty. I have tried to add
use WithMedia;
public $mediaComponentNames = ['main_image'];
public $main_image;
use WithMedia;
public $mediaComponentNames = ['main_image'];
public $main_image;
but no changes are. Any thought?
13 Replies
toeknee
toeknee10mo ago
How are you gettin the data? it should be with form->getState() for it to process the images correctly. You shouldn't need to manually add the media etc
tesse05
tesse05OP10mo ago
hm, doing form->getState() it doesn't appear the media input attribute🤔
toeknee
toeknee10mo ago
by doing that it handles it behind the scenes and shouldn't need to to do anything
tesse05
tesse05OP10mo ago
it doesn't work at all... doing a dd() of form state the 'main_image' attribute it doesn't appear and it's not saving neither
No description
tesse05
tesse05OP10mo ago
I found the trick. I was not assigning the form model...
toeknee
toeknee10mo ago
That'll do it!
tesse05
tesse05OP10mo ago
Is there any trick to assign it dynamically? For example to use the same form with different models
toeknee
toeknee10mo ago
So the model shouldn't need assigning looking at it. have you applied the has media to the model? The model you are saving the form data too, should implements HasMedia and use InteractsWithMedia That should be enough for the field to assign it to that model.
tesse05
tesse05OP10mo ago
I have this use case
toeknee
toeknee10mo ago
Oh ok interesting I am unsure quite how to handle that I assume you are assigning the model with ->model() ? if so, can you pass the model in when calling the edit function?
tesse05
tesse05OP10mo ago
I tried this: $this->form->model($this->model) but not working 😦 is passing all fields except media
toeknee
toeknee10mo ago
And each model you are trying to save the image against implements HasMedia and use InteractsWithMedia
tesse05
tesse05OP10mo ago
yes
Want results from more Discord servers?
Add your server