F
Filament12mo ago
BEVER

Problems with SpatieMediaLibraryFileUpload on a modal action.

Hi everybody, I'm trying to add a media uploader on a CreateAction, the usecase here is to add some files to a note being added to a resource. I'm using the following code ( see screenshot). Problem is that the media gets saved on the parent model, instead of my Note::class model. Does anybody know how this can be fixed?
No description
8 Replies
BEVER
BEVEROP12mo ago
@ralphjsmit could you help me with this?
ralphjsmit
ralphjsmit12mo ago
Hi! I don't maintain the SpatieMediaLibraryFileUpload plugin, but only the MediaLibrary plugin: https://filamentphp.com/plugins/ralphjsmit-media-library-manager. Perhaps that plugin works better for your use case :)
toeknee
toeknee12mo ago
you can pass in: ->model() and pass in the note model.
BEVER
BEVEROP12mo ago
@toeknee , that doesn't do anything as I don't know the record yet The action creates a note, with the media attached to the note but it attaches it to the parent model. When attaching ->model(Note::class) to the medialibrary field, nothing gets saved.
toeknee
toeknee12mo ago
Ahh ok, so you are trying to save to the relationship note_attachements on notes
BEVER
BEVEROP12mo ago
correct
toeknee
toeknee12mo ago
We have that working here, one second] me and @Diogo Pinto (mainly them) created a re-usable notes action. Ok I see how we are handling it here. We use FileUpload but after file upload we then call the AddMedia method from spatie
->action(function (array $data, $record, $form) {
$note = $record->filament_notes()->create([
'note_body' => $data['note_body'],
'user_id' => auth()->user()->id,
'team_id' => auth()->user()->current_team_id,
'action_date' => $data['action_date'] ?? null,
'is_pinned' => $data['is_pinned'] ?? false,
]);
if (isset($data['media'])) {
foreach ($data['media'] as $file) {
$fileAdder = $note->addMedia(storage_path('app/public/'.$file))
->toMediaCollection('notes');

$fileAdder->file_name = md5($fileAdder->uuid).'.'.$fileAdder->extension;
$fileAdder->save();
}
}
Notifications\Notification::make()
->title(__('Note added!'))
->success()
->send();
$form->fill();
$this->halt();
})
->action(function (array $data, $record, $form) {
$note = $record->filament_notes()->create([
'note_body' => $data['note_body'],
'user_id' => auth()->user()->id,
'team_id' => auth()->user()->current_team_id,
'action_date' => $data['action_date'] ?? null,
'is_pinned' => $data['is_pinned'] ?? false,
]);
if (isset($data['media'])) {
foreach ($data['media'] as $file) {
$fileAdder = $note->addMedia(storage_path('app/public/'.$file))
->toMediaCollection('notes');

$fileAdder->file_name = md5($fileAdder->uuid).'.'.$fileAdder->extension;
$fileAdder->save();
}
}
Notifications\Notification::make()
->title(__('Note added!'))
->success()
->send();
$form->fill();
$this->halt();
})
the form field
Forms\Components\FileUpload::make('media')
->visibility('private')
->preserveFilenames()
->dehydrated(true)
->multiple()
->previewable(false)
->columnSpanFull()
->label(__('Note Media/File(s)')),
Forms\Components\FileUpload::make('media')
->visibility('private')
->preserveFilenames()
->dehydrated(true)
->multiple()
->previewable(false)
->columnSpanFull()
->label(__('Note Media/File(s)')),
BEVER
BEVEROP12mo ago
@toeknee thanks for the help, will try that!
Want results from more Discord servers?
Add your server