F
Filament17mo ago
rabol

How do I get the file in a upload action

on a resource page I have an action:
Tables\Actions\Action::make('import')
->form([
Forms\Components\FileUpload::make('file'),
])
->label('')
->tooltip('Import')
->icon('heroicon-o-folder-open')
->action('import'),
Tables\Actions\Action::make('import')
->form([
Forms\Components\FileUpload::make('file'),
])
->label('')
->tooltip('Import')
->icon('heroicon-o-folder-open')
->action('import'),
shows a modal, one can upload a file and it call my 'import' method. But... what parameters do I get in my method? I would lige to get the current record and then the fileupload so that I can 'import' the data in this case
public function import($record, $file)
{

dd($record, $file);
}
public function import($record, $file)
{

dd($record, $file);
}
Does not work
Solution:
Here is the solution ``` Tables\Actions\Action::make('import') ->form([...
Jump to solution
6 Replies
Dennis Koch
Dennis Koch17mo ago
It's described in the docs. You need $data https://filamentphp.com/docs/2.x/tables/actions#custom-forms
Filament
Actions - Table Builder - Filament
The elegant TALL stack table builder for Laravel artisans.
rabol
rabolOP17mo ago
well... $data only contains the filename, not the FileUpload object so it's difficult to 'move' or read the file and I do not have a callback for the action as I use in the Admin panel on a resource page
Dennis Koch
Dennis Koch17mo ago
Yes that’s right. But you shouldn’t need more for an import? If you need the FileUpload object I think you can hook into saveFilesUsing()
rabol
rabolOP17mo ago
I know that the file will be in the livewire tmp, but... that is 'hard-coding' 😀 eg. File:get() or file_get_contents() need the full path
Solution
rabol
rabol17mo ago
Here is the solution
Tables\Actions\Action::make('import')
->form([
FileUpload::make('file')
->label('')
->acceptedFileTypes(['application/json', 'json'])
->imagePreviewHeight('250')
->reactive()
->afterStateUpdated(function (callable $set, TemporaryUploadedFile $state) {
$set('fileRealPath', $state->getRealPath());
}),
Hidden::make('fileRealPath'),
])
->label('')
->tooltip('Import')
->icon('heroicon-o-folder-open')
->action('import'),
Tables\Actions\Action::make('import')
->form([
FileUpload::make('file')
->label('')
->acceptedFileTypes(['application/json', 'json'])
->imagePreviewHeight('250')
->reactive()
->afterStateUpdated(function (callable $set, TemporaryUploadedFile $state) {
$set('fileRealPath', $state->getRealPath());
}),
Hidden::make('fileRealPath'),
])
->label('')
->tooltip('Import')
->icon('heroicon-o-folder-open')
->action('import'),
The trick is to store the realpath in a hidden input
jsn1nj4
jsn1nj417mo ago
@steen.rabol just wanted to say, thanks, this helped me figure out what I needed for my image uploads too
Want results from more Discord servers?
Add your server