benjji92
FileUpload doesnt work properly
I have an action that uploads an excel file and run a script with the data in it, then deletes the uploaded file since its no longer needed.
->actions([
Tables\Actions\Action::make('Run')
->label('Run script')
->color('danger')
->size('lg')
->icon('heroicon-o-play')
->requiresConfirmation()
->form([
FileUpload::make('excel')
->label('Excel')
->acceptedFileTypes(['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'])
->preserveFilenames(),
])
->action(function ($data) {
$path = $data['excel'];
$deleteScript = new Script();
$deleteScript->delete($path);
Storage::delete($path);
})
class DeleteScript
{
public function delete($path)
{
$data = Excel::toArray([], $path);
foreach ($data as $row) {
$videoCode = trim(implode('', $row[0]));
if(empty($videoCode)){
continue;
}
//Log::info("videocode: $videoCode");
$video = Videos::where('code', $videoCode)->delete();
}
}
}
For example: I upload a delete.xlsx file, but I get the following error:
Could not find zip member zip://C:\laragon\www\fanny\storage\framework\cache\laravel-excel\laravel-excel-yBCw78HjXxAlznjDQ609T9mkdHe52RYz.xlsx#_rels/.rels
I found out that if I place this delete.xlsx in the \storage\app folder, the code runs without error, succesfully deleting the data given in the excel file.
How is this related? The uploaded excel file goes to the \storage\app\public folder.
What goes wrong here?3 replies
still struggling to pass data from FileUpload in filament resource to my command script
As the title suggests, I would like my script to read the data and run, according to excels I upload. Currently it only works with hardcoded excel files that I store in storage/app/public. Here is a github gist link to my code.
https://gist.github.com/Benjji92/42839a5088fb052e624c4d480672e94e
Any help would be much appriciated as Im struggling to do this for the past days.
13 replies
how to pass uploaded file as variable in table builder?
public static function table(Table $table): Table
{
return $table
->columns([
//
])
->filters([
//
])
->actions([
Tables\Actions\Action::make('Run')
->label('Run script')
->color('danger')
->size('lg')
->icon('heroicon-o-play')
->requiresConfirmation()
->form([
SpatieMediaLibraryFileUpload::make('excel')
->label('Excel')
->collection('excel')
->preserveFilenames(),
])
->action (function (Request $request) {
$command = new \App\Console\Commands\DeleteData();
$file = $request->file('excel');
return $command->handle($file);
})
])
->bulkActions([
//
]);
}
i would like to pass the excel file i upload here to the deletescript, but i dont understand how this works
class DeleteScript
{
public function delete()
{
$data = Excel::toArray([], $file);
$dataChunks = array_chunk($data[0], 25);
foreach ($dataChunks as $chunk) {
//rest of the code
}
}
}
class DeleteData extends Command
{
protected $signature = 'delete:data';
protected $description = 'Delete Data';
public function handle()
{
(new DeleteScript())->delete();
return Command::SUCCESS;
}
}
17 replies