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
It's
$data
not $request
: ->action (function ($data) {
$data
is an array that will contain the filenamethank you i corrected it, but i still dont understand how could i pass the uploaded excel to the script
What’s the issue?
It should be an array with a single value
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;
}
Your class is not accepting any arguments. Where should data come from if you don’t pass it to your script?
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.
it doesnt contain anything
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.
https://gist.github.com/Benjji92/42839a5088fb052e624c4d480672e94e
i used SpateMediaFileUpload everywhere else in the code, thats why i also used it here
Where is the
dd()
that doesn't print anything? DeleteData::handle()
still doesn't accept any attributes.oh sry i deleted that its here
Yeah, probably related to SpatieMediaLibrary then. Have you tried without it?
not yet, didnt think it would be the problem, ill try without it now
indeed it was spatie 😮