How to pass an argument to a custom table actions

->actions([
Tables\Actions\EditAction::make(),
\Filament\Tables\Actions\Action::make('Generate QR Codes')
->label('QRCodes')
->action('generateAndDownloadQRCodes')
->color('success')
// ->record(fn($record) => $record->id)
->arguments(fn($record) => ['id' => $record->id])
])
->actions([
Tables\Actions\EditAction::make(),
\Filament\Tables\Actions\Action::make('Generate QR Codes')
->label('QRCodes')
->action('generateAndDownloadQRCodes')
->color('success')
// ->record(fn($record) => $record->id)
->arguments(fn($record) => ['id' => $record->id])
])
how to pass arguments to a custom action, search the entire documentation did see anything mentioned regarding this
Solution:
So you want: ```php Tables\Actions\EditAction::make(), \Filament\Tables\Actions\Action::make('Generate QR Codes')...
Jump to solution
8 Replies
toeknee
toeknee3w ago
What are you trying to do. is this in the action for submitting the action,or using it within somewhere else within the action like form
Obala
ObalaOP3w ago
I want to allow users to generate QR codes for all tickets related to a specific event directly from the EventResource index page. The QR codes will: Be unique to each ticket. Be saved as images in storage. Be bundled into a downloadable ZIP file for the user. The action button will: Trigger the generation of QR codes. Handle the association with the event automatically via the passed id.
Solution
toeknee
toeknee3w ago
So you want:
Tables\Actions\EditAction::make(),
\Filament\Tables\Actions\Action::make('Generate QR Codes')
->label('QRCodes')
->action('generateAndDownloadQRCodes')
->color('success')
->action(function($record){
$ticketQrCodes =[];
foreach($record->tickets as $ticket) {
$ticketQrCodes[$ticket->id] = $ticket->qrCodeFile;
}
// Code to bundled the tickets from ticketQrCodes array.

$myZip = ziparchiverclass;

return response()->stream(function () use ($myzip) {
echo file_get_contents($myzip);
}, 200, $headers);
})
Tables\Actions\EditAction::make(),
\Filament\Tables\Actions\Action::make('Generate QR Codes')
->label('QRCodes')
->action('generateAndDownloadQRCodes')
->color('success')
->action(function($record){
$ticketQrCodes =[];
foreach($record->tickets as $ticket) {
$ticketQrCodes[$ticket->id] = $ticket->qrCodeFile;
}
// Code to bundled the tickets from ticketQrCodes array.

$myZip = ziparchiverclass;

return response()->stream(function () use ($myzip) {
echo file_get_contents($myzip);
}, 200, $headers);
})
Obala
ObalaOP3w ago
the action is on single row of items in the table, meaning that like and edit link on the table to links to a particular resource, the action also generates tickets for a single event
toeknee
toeknee3w ago
Thats what the above would do... You just need to code in the ticket generator
Obala
ObalaOP3w ago
this is how i wrote it initially
public function generateAndDownloadQRCodes(): \Symfony\Component\HttpFoundation\StreamedResponse
{
$events = CardTicket::all(); // Replace with your model
$tempDir = storage_path('app/qr_temp/');
$zipFilePath = storage_path('app/qr_codes.zip');

// Ensure temp directory exists
if (!file_exists($tempDir)) {
mkdir($tempDir, 0777, true);
}

// Generate QR codes for each event
foreach ($events as $event) {
$qrContent = json_encode([
'event_name' => $event->event_name,
'event_date' => $event->event_date,
'barcode' => $event->barcode,
]);

$fileName = $tempDir . Str::slug($event->event_name . '-' . $event->id) . '.png';

QrCode::format('png')
->size(300)
->generate($qrContent, $fileName);
}

// Create a ZIP file
$zip = new ZipArchive();
if ($zip->open($zipFilePath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
$files = glob($tempDir . '*.png');
foreach ($files as $file) {
$zip->addFile($file, basename($file));
}
$zip->close();
}

// Clean up temporary QR code files
array_map('unlink', glob($tempDir . '*.png'));
rmdir($tempDir);

// Return ZIP file as a download response
return response()->streamDownload(function () use ($zipFilePath) {
echo file_get_contents($zipFilePath);
}, 'qr_codes.zip', [
'Content-Type' => 'application/zip',
]);
}
public function generateAndDownloadQRCodes(): \Symfony\Component\HttpFoundation\StreamedResponse
{
$events = CardTicket::all(); // Replace with your model
$tempDir = storage_path('app/qr_temp/');
$zipFilePath = storage_path('app/qr_codes.zip');

// Ensure temp directory exists
if (!file_exists($tempDir)) {
mkdir($tempDir, 0777, true);
}

// Generate QR codes for each event
foreach ($events as $event) {
$qrContent = json_encode([
'event_name' => $event->event_name,
'event_date' => $event->event_date,
'barcode' => $event->barcode,
]);

$fileName = $tempDir . Str::slug($event->event_name . '-' . $event->id) . '.png';

QrCode::format('png')
->size(300)
->generate($qrContent, $fileName);
}

// Create a ZIP file
$zip = new ZipArchive();
if ($zip->open($zipFilePath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
$files = glob($tempDir . '*.png');
foreach ($files as $file) {
$zip->addFile($file, basename($file));
}
$zip->close();
}

// Clean up temporary QR code files
array_map('unlink', glob($tempDir . '*.png'));
rmdir($tempDir);

// Return ZIP file as a download response
return response()->streamDownload(function () use ($zipFilePath) {
echo file_get_contents($zipFilePath);
}, 'qr_codes.zip', [
'Content-Type' => 'application/zip',
]);
}
toeknee
toeknee3w ago
You are wanting tickets per event. Conditiong the CardTicket to use $this->id and put it in your model as a function which returns the zip file, then on streaming in the action delete after download.
Obala
ObalaOP3w ago
and thank you, you got me somewhere, the main issue was of passing the argument to the function used to generate the qr code, now that i can write the function inside the action its self pretty much solves my problem
Want results from more Discord servers?
Add your server