F
Filament16mo ago
Darpan

How to use SpatieMediaLibraryFileUpload in hintAction?

I Have a hint action to create new record like:
->hintAction(
Action::make('create_amenity')
->form([
TextInput::make('name'),
SpatieMediaLibraryFileUpload::make('image'),
])
->action(function ($data) {
dd($data);
// create amenity
}),
),
->hintAction(
Action::make('create_amenity')
->form([
TextInput::make('name'),
SpatieMediaLibraryFileUpload::make('image'),
])
->action(function ($data) {
dd($data);
// create amenity
}),
),
but I only get name in $data array, how can I access image?
9 Replies
Darpan
DarpanOP16mo ago
Possible?
toeknee
toeknee16mo ago
It should exist within $data? Otherwise use: $this->getState
Darpan
DarpanOP16mo ago
No, it does not exist in $data, the normal FileUpload works and shows file name in $data but not SpatieMediaLibraryFileUpload. I can't use $this, getting error Using $this when not in object context The following works but I don't think this is the best way to do this.
->action(function ($livewire) {
$data = $livewire->mountedFormComponentActionsData[0];
$image = array_values($data['image'])[0];
$amenity = PropertyAmenity::create([
'name' => $data['name'],
]);

$amenity
->addMedia($image)
->toMediaCollection('image');
})
->action(function ($livewire) {
$data = $livewire->mountedFormComponentActionsData[0];
$image = array_values($data['image'])[0];
$amenity = PropertyAmenity::create([
'name' => $data['name'],
]);

$amenity
->addMedia($image)
->toMediaCollection('image');
})
ralphjsmit
ralphjsmit16mo ago
Try it like this:
->hintAction(
Action::make('create_amenity')
->form([
TextInput::make('name'),
SpatieMediaLibraryFileUpload::make('image'),
])
->action(function (Form $form, $record) {
$form->model($record)->saveRelationships();
}),
),
->hintAction(
Action::make('create_amenity')
->form([
TextInput::make('name'),
SpatieMediaLibraryFileUpload::make('image'),
])
->action(function (Form $form, $record) {
$form->model($record)->saveRelationships();
}),
),
Darpan
DarpanOP16mo ago
Tried this, it does nothing. No record is created.
DrByte
DrByte16mo ago
On your PropertyAmenity model, have you created a registerMediaCollections() method to auto-link the files to the model?
public function registerMediaCollections(): void
{
$this->addMediaCollection('my-collection')
//add options
...
public function registerMediaCollections(): void
{
$this->addMediaCollection('my-collection')
//add options
...
Ref: https://spatie.be/docs/laravel-medialibrary/v10/working-with-media-collections/defining-media-collections
wyChoong
wyChoong16mo ago
is your hint in a create form?
ralphjsmit
ralphjsmit16mo ago
Oh, you need to create a record?
->hintAction(
Action::make('create_amenity')
->form([
TextInput::make('name'),
SpatieMediaLibraryFileUpload::make('image'),
])
->action(function (Form $form, array $data) {
$amenity = Amenity::create($data);

$form->model($amenity)->saveRelationships();
}),
),
->hintAction(
Action::make('create_amenity')
->form([
TextInput::make('name'),
SpatieMediaLibraryFileUpload::make('image'),
])
->action(function (Form $form, array $data) {
$amenity = Amenity::create($data);

$form->model($amenity)->saveRelationships();
}),
),
Darpan
DarpanOP16mo ago
This works, thanks!

Did you find this page helpful?