F
Filament5mo ago
Nicole

Dumping $data doesn't include the SpatieMediaLibraryFileUpload value.

$data doesn't include the thumbnail from SpatieMediaLibraryFileUpload.
10 Replies
Nicole
NicoleOP5mo ago
Here's my current code
class ProductsManager extends Field
{
protected string $view = 'filament.forms.components.products-manager';

protected function setUp(): void
{
parent::setUp();

$this->registerActions([
fn (ProductsManager $component): Action => $component->getAddProductAction(),
]);
}

public function getAddProductAction(): Action
{
return Action::make('addProduct')
->label('+ Add a Product')
->color('primary')
->size('sm')
->modalHeading('Add a Product')
->modalSubmitActionLabel('Save')
->modalCancelActionLabel('Cancel')
->form([
Forms\Components\TextInput::make('name')
->label('Name of Product')
->placeholder('Top Waterfronts and Beaches')
->required(),

Forms\Components\SpatieMediaLibraryFileUpload::make('thumbnail')
->label('Photo Thumbnail')
->collection(MediaCollection::PRODUCT_THUMBNAIL->value)
->visibility('private')
->maxSize(2000)
->acceptedFileTypes(['image/png', 'image/svg', 'image/jpeg', 'image/jpg'])
->required()
->preserveFilenames(),
])
->action(function (array $data, ProductsManager $component): void {

$state = $component->getState() ?? [];
$state[] = $data;

dd($state);

$component->state($state);
});
}
}
class ProductsManager extends Field
{
protected string $view = 'filament.forms.components.products-manager';

protected function setUp(): void
{
parent::setUp();

$this->registerActions([
fn (ProductsManager $component): Action => $component->getAddProductAction(),
]);
}

public function getAddProductAction(): Action
{
return Action::make('addProduct')
->label('+ Add a Product')
->color('primary')
->size('sm')
->modalHeading('Add a Product')
->modalSubmitActionLabel('Save')
->modalCancelActionLabel('Cancel')
->form([
Forms\Components\TextInput::make('name')
->label('Name of Product')
->placeholder('Top Waterfronts and Beaches')
->required(),

Forms\Components\SpatieMediaLibraryFileUpload::make('thumbnail')
->label('Photo Thumbnail')
->collection(MediaCollection::PRODUCT_THUMBNAIL->value)
->visibility('private')
->maxSize(2000)
->acceptedFileTypes(['image/png', 'image/svg', 'image/jpeg', 'image/jpg'])
->required()
->preserveFilenames(),
])
->action(function (array $data, ProductsManager $component): void {

$state = $component->getState() ?? [];
$state[] = $data;

dd($state);

$component->state($state);
});
}
}
Majid Al Zariey
Majid Al Zariey5mo ago
relationships are not saved in the $data array, you could save them
$product = Product::create($form->getState());

// Save the relationships from the form to the product after it is created.
$form->model($product)->saveRelationships();
$product = Product::create($form->getState());

// Save the relationships from the form to the product after it is created.
$form->model($product)->saveRelationships();
as discussed here https://filamentphp.com/docs/3.x/forms/adding-a-form-to-a-livewire-component#passing-the-form-model-after-the-form-has-been-submitted
Nicole
NicoleOP5mo ago
The idea Sir is to get the uploaded file and process it manually.
Majid Al Zariey
Majid Al Zariey5mo ago
first attach them to the model the process them could you give an example of what processing is needed for the images
Nicole
NicoleOP5mo ago
Technically Sir, the action there is not supposed to save the media yet. I still need to pass the data I get there to the parent component, which is a repeater. The issue is that all the data is being received, except for the uploaded media file — it's missing when I dump dd($this->data).
Majid Al Zariey
Majid Al Zariey5mo ago
Its how SpatieMediaLibraray Works Its a relation based connected to the model which needs to be created to be attached Using a FileUpload field instead would work with you
Winter Is Coming
add ->dehydrated maybe? but dont forget to unset it before save
LeandroFerreira
LeandroFerreira5mo ago
if you pass SpatieMediaLibraryFileUpload::make('thumbnail')->dehydrated(), I think you can access the TemporaryUploadedFile in the $data array. You can also try saveUploadedFileUsing using a FileUpload field
FileUpload::make('thumbnail')
->saveUploadedFileUsing(function (TemporaryUploadedFile $file) {
// $file...
})
FileUpload::make('thumbnail')
->saveUploadedFileUsing(function (TemporaryUploadedFile $file) {
// $file...
})
Nicole
NicoleOP5mo ago
By the way, I access the image like this $actionData = $livewire->mountedFormComponentActionsData[0] ?? []; not sure if it is a good approach though.
LeandroFerreira
LeandroFerreira5mo ago
no problem, I think

Did you find this page helpful?