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; } }
14 Replies
Dennis Koch
Dennis Koch2y ago
It's $data not $request: ->action (function ($data) { $data is an array that will contain the filename
benjji92
benjji92OP2y ago
thank you i corrected it, but i still dont understand how could i pass the uploaded excel to the script
Dennis Koch
Dennis Koch2y ago
What’s the issue? It should be an array with a single value
benjji92
benjji92OP2y ago
i dont understand how can i pass the uploaded file as data to the script, like: 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 ( $data) { $command = new \App\Console\Commands\DeleteData(); return $command->handle($data); }) i upload the excel in this form, and want the action to use that excel as data and work the script from that but in my DeleteScript class i keep getting Undefined variable $data error: class DeleteScript { public function delete() { $data = Excel::toArray([], $data); ...rest of the code... in my DeleteData class: public function handle() { (new DeleteScript())->delete(); return Command::SUCCESS; }
Dennis Koch
Dennis Koch2y ago
Your class is not accepting any arguments. Where should data come from if you don’t pass it to your script?
benjji92
benjji92OP2y ago
Originally I hardcoded an xlsx file in the DeleteScript class and thats where the data was coming from, and I run the command for the script in the terminal. I wanted to change this so I dont have to touch the code and run the terminal every time I want to work with a new excel file, but just upload it through the admin panel and run the script from there.
benjji92
benjji92OP2y ago
it doesnt contain anything
Dennis Koch
Dennis Koch2y ago
Show your code. Btw. why are you using SpateMediaFileUpload? Not sure whether that's the issue, but I think that will save directly to the media relation.
benjji92
benjji92OP2y ago
https://gist.github.com/Benjji92/42839a5088fb052e624c4d480672e94e i used SpateMediaFileUpload everywhere else in the code, thats why i also used it here
Gist
DeleteData.php
GitHub Gist: instantly share code, notes, and snippets.
Dennis Koch
Dennis Koch2y ago
Where is the dd() that doesn't print anything? DeleteData::handle() still doesn't accept any attributes.
benjji92
benjji92OP2y ago
oh sry i deleted that its here
Dennis Koch
Dennis Koch2y ago
Yeah, probably related to SpatieMediaLibrary then. Have you tried without it?
benjji92
benjji92OP2y ago
not yet, didnt think it would be the problem, ill try without it now
benjji92
benjji92OP2y ago
indeed it was spatie 😮
Want results from more Discord servers?
Add your server