Action button url for a post request

Hi all is there a way.to use an action button in javascript for a post request. It seems it only supports get requests?
24 Replies
krekas
krekas15mo ago
what exactly are you trying to achieve?
James | mrtorks[SH Aron]
I am trying to post to a url on button click like so
new NotificationAction('Proceed')
.button()
.url(route('delete-attachments', { attachmentsIds: [attachmentId]}))
.close(),
new NotificationAction('Proceed')
.button()
.url(route('delete-attachments', { attachmentsIds: [attachmentId]}))
.close(),
wyChoong
wyChoong15mo ago
because ->url is just a <a> with href use ->action() and do your Http::post in there, but the better way is to extract it out as an action instead of calling http in your code
James | mrtorks[SH Aron]
this is being used in javascript so that approach would not work unfortunately
wyChoong
wyChoong15mo ago
You can call php function from@javascript with $wirr
BloodDrunk
BloodDrunk7mo ago
Bumping the thread since I have very similar question:
return [
Actions\Action::make("download_pdf")
->label("Download PDF")
->icon("heroicon-o-arrow-down-tray")
->action(function (){
$response = Http::post(route("generate_pdf"), [
'pdf_template_path' => "pdf-templates.merchant-invoices",
'pdf_data' => [
'invoices' => Statement::all(),
],
]);
}),
];
return [
Actions\Action::make("download_pdf")
->label("Download PDF")
->icon("heroicon-o-arrow-down-tray")
->action(function (){
$response = Http::post(route("generate_pdf"), [
'pdf_template_path' => "pdf-templates.merchant-invoices",
'pdf_data' => [
'invoices' => Statement::all(),
],
]);
}),
];
This doesn't seem to do anything although its what you have recommended. The controller responsible for that route initiates file download, but I can't get the controller to run. What are my options? A more general question is, how can one use filamen't actions in order to make a request to the endpoint and send data along the request.
krekas
krekas7mo ago
livewire needs to return response check the livewire docs
BloodDrunk
BloodDrunk7mo ago
Can you be more descriptive? Perhaps pointing me to which part of the documentation in the Livewire site would be helpful, as I am not finding anything related to my problem in livewire's "retuning response" search. As far as I have understood, the request never reaches the controller, and thus download can never occur, in network tab all I see is "update" on a livewire component
krekas
krekas7mo ago
Laravel
File Downloads | Laravel
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
krekas
krekas7mo ago
And http isn't meant for what you are trying to use
BloodDrunk
BloodDrunk7mo ago
I have already went throught both of those examples, thank you for the response! The thing is, I cant do it that way, this uses a per-record actions and response download of a file that is stored on the system. I already have the pdf service which generates pdf from template and the data passed to it, pdf controller which manages that, route which points to the controller I specifically need to be able to cause a post request to the route with the data, on the header button click (the button which is where usually a "create new X" is). I need new header action button, with what I mentioned. Is there a possibility of doing that, or how else should I approach that problem? I am trying to not leave filament pages, but so far no luck, and thinking I m going to have to resort to custom pages.
krekas
krekas7mo ago
What custom page would do different? And why do you need post? What's so different about it?
BloodDrunk
BloodDrunk7mo ago
Custom page would allow me complete control over the page, layout (such as a custom button), to which i can bind javascript function and comstruct a post request, but that seems like a lot of rework for such a simple functionality that I believe shouldnt be so hard to implement on existing system. I explained above that i need to send the data for the pdf template for the service to construct the pdf. Such as path to the template and the data for the template.
krekas
krekas7mo ago
Why can't you do that with get?
BloodDrunk
BloodDrunk7mo ago
I dont know, why can't I send a collection of objects of a model through get? Ultimatively what I'm doing is sending as a data a collection of objects, where would a GET store that
krekas
krekas7mo ago
send only ids as an array for example
BloodDrunk
BloodDrunk7mo ago
That would require me to implement a fetching method from inside the controller or even a service, which I want to avoid, and the service is already working for other pdf generation pages I have, so I'd like to keep it as it is right now. I guess the answer is: it isn't possible right now. Maybe it can be added in new filament versions @Dan Harrin (sorry for the tag, just wanted to involve you for reading). Thanks for your time
Dan Harrin
Dan Harrin7mo ago
I don't understand why you wouldn't just abstract our the download functionality into a service class, which can then be called from that action function as well as the controller?
BloodDrunk
BloodDrunk7mo ago
I could do that but then I would have to rework all other pages and how the data is sent and parsed, it's not a problem but its just more work in order to achieve my goal, kind of remeding the issue instead of addressing the root cause. So in essence, header actions cannot (yet) post data to controller?
Dan Harrin
Dan Harrin7mo ago
I've genuinely never heard this request before because it's a framework built on Livewire where pretty much everyone is following the proper conventions there
BloodDrunk
BloodDrunk7mo ago
No, that's alright, I'm not very familiar with filament and livewire, that is why I am asking if there is a possibility of achieving my goal, could be my approach to the functionality is wrong
Dan Harrin
Dan Harrin7mo ago
I see a refactor as addressing your root issue and not the lack of POST support in actions the refactor is a better long term solution by far IMO
BloodDrunk
BloodDrunk7mo ago
Yeah, might have to take that option into consideration. Thank you for your input tho! Just putting it here for anyone else having similar problem: I had to resort to making a custom header component, importing existing components (such as breadcrumbs) to have the header look as close as normal resource page, and using filament::buttons with custom javascript code to achieve my goal. It's much less work and works well!
Want results from more Discord servers?
Add your server