-
hi, mi focus is connect a button to a method for download documents generated on the fly:
in Screen class downloadContract can do this? |
Beta Was this translation helpful? Give feedback.
Answered by
tabuna
Jan 5, 2023
Replies: 1 comment 1 reply
-
If you want the "Download" button to be on several screens at once, but execute code as the click was only in one place, there are several ways:
If you just want to pass a parameter, it would be like this: Button::make('Export file')
->method('export', [
'name' => "Any name for file",
])
->icon('cloud-download')
->rawClick()
->novalidate(), and method screen: /**
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function export($name)
{
return response()->streamDownload(function () {
$csv = tap(fopen('php://output', 'wb'), function ($csv) {
fputcsv($csv, ['header:col1', 'header:col2', 'header:col3']);
});
collect([
['row1:col1', 'row1:col2', 'row1:col3'],
['row2:col1', 'row2:col2', 'row2:col3'],
['row3:col1', 'row3:col2', 'row3:col3'],
])->each(function (array $row) use ($csv) {
fputcsv($csv, $row);
});
return tap($csv, function ($csv) {
fclose($csv);
});
}, "$name.csv");
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
lrnzfrr
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want the "Download" button to be on several screens at once, but execute code as the click was only in one place, there are several ways:
formaction
instead of a methodIf you just want to pass a parameter, it would be like this:
and method screen: