File Upload within a custom page is null

So My page resource
<x-filament-panels::form wire:submit="submit">
{{ $this->form }}
<div>
<x-filament::button type="submit" size="sm" wire:loading.attr="disabled" wire:target="submit"
wire:loading.class="opacity-50 cursor-not-allowed">
Verify
</x-filament::button>
</div>
</x-filament-panels::form>
<x-filament-panels::form wire:submit="submit">
{{ $this->form }}
<div>
<x-filament::button type="submit" size="sm" wire:loading.attr="disabled" wire:target="submit"
wire:loading.class="opacity-50 cursor-not-allowed">
Verify
</x-filament::button>
</div>
</x-filament-panels::form>
And my form im using the spatie media library file upload component
/**
* Form Schema
*
* @param Form $form
* @return Form
*/
public function form(Form $form): Form
{
return $form->schema([
TextInput::make('name'),
SpatieMediaLibraryFileUpload::make('media')
])
->statePath('data');
}

public function submit()
{
$data = $this->form->getState();
dd($data);
}
/**
* Form Schema
*
* @param Form $form
* @return Form
*/
public function form(Form $form): Form
{
return $form->schema([
TextInput::make('name'),
SpatieMediaLibraryFileUpload::make('media')
])
->statePath('data');
}

public function submit()
{
$data = $this->form->getState();
dd($data);
}
But in my dd() I only see name, there is nothing regarding the file upload part. Is there something im missing that I cant see in the documentation?
Solution:
So something like this ```php public function submit() { $model = MyModel::create($this->form->getState());...
Jump to solution
9 Replies
Jamie Cee
Jamie CeeOP2w ago
So, if I use FileUpload::make(), it appears to work, but the Spatie one doesnt... but the documentation says the Spatie one is used the same way as the FileUpload component?
Jamie Cee
Jamie CeeOP2w ago
No description
ChesterS
ChesterS2w ago
Try $data = $this->form->getRawState(); Also look at the FileUpload::getUploadedFileNameForStorage. Or you can override the fileupload process using ->saveUploadedFileUsing() Anyway yeah file uploads are a bit of a pain if you want to do custom stuff.
Jamie Cee
Jamie CeeOP2w ago
So does using getRawState() not handle the actual uploading part? Assuming Id then have to do that overriding to manage how and where data is uploaded?
ChesterS
ChesterS2w ago
I'm not sure what you want to do so I just posted some stuff that might help. A lot of the details depend on other configs - eg what's the default storage? Is it locally or some other service like S3? Do you want to override the upload process entirely or just do something else after the upload is done? I don't remember much, but here is a snippet of how I used the raw data to send an email with the file uploads as attachemts
->action(function ($record, array $data, Form $form) {
$data['attachments'] = $form->getRawState()['media'];
SendSomeEmail::dispatch($record, $data);
})
->action(function ($record, array $data, Form $form) {
$data['attachments'] = $form->getRawState()['media'];
SendSomeEmail::dispatch($record, $data);
})
and then
$extraAttachments = Media::whereIn('uuid', $data['attachments'] ?? [])->get();

/** @var Media $attachment */
foreach ($extraAttachments as $attachment) {
$mail->attach($attachment->toMailAttachment(), ['as' => $attachment->getFullName()]);
}
$extraAttachments = Media::whereIn('uuid', $data['attachments'] ?? [])->get();

/** @var Media $attachment */
foreach ($extraAttachments as $attachment) {
$mail->attach($attachment->toMailAttachment(), ['as' => $attachment->getFullName()]);
}
There might be a better way to do this, but I hope this helps in what you're trying to do
Jamie Cee
Jamie CeeOP2w ago
The main use is default, it's just trying to put it on a custom page, rather than a resource. So I've setup the form as normal, and im trying to use the spatie media library (https://filamentphp.com/plugins/filament-spatie-media-library#form-component). But the basic usage set out in the docs doesn't appear to be working. Not adding anything extra currently
ChesterS
ChesterS2w ago
So you just want to save the file? Do you have a $this->form->model($model)->saveRelationships(); call?
Solution
ChesterS
ChesterS2w ago
So something like this
public function submit()
{
$model = MyModel::create($this->form->getState());
$this->form->model($model)->saveRelationships();
}
public function submit()
{
$model = MyModel::create($this->form->getState());
$this->form->model($model)->saveRelationships();
}
Jamie Cee
Jamie CeeOP2w ago
So you just want to save the file?
At present, yes. When I use the SpatieMediaLibraryFileUpload component (from spatie docs), my data from form->getState or getRawState is null, but if I use Filaments default FIleUpload, it returns string as normal etc. Im still trring to associate that file with the Spatie media mainly for file information etc, but just cant obtain the 'document' Not using saveRelationships as of yet, will give that a go Yeah, so the combination of the relationship, and changing the morphs model to be a uuid, looks like its working. Thank you

Did you find this page helpful?