F
Filament12mo ago
ChrisB

Multiple Actions on Save

I'm new to Filament, and Laravel in general...sorry in advance if I've missed something elementary. I've got a long way on my own, but hit a brick wall on this one. Resource setup (with relationship too, but unrelated for this question), and working perfectly for creating, deleting and editing. At this point, my question relates to being in an existing record and making changes. When pressing save, I want to save the record + fire off a webhook to trigger a third party. I've fired up Webhooks, and can a call it from a separate action button on my form. I just can't combine them! The below code is located in my ''Edit Page' of the resource, and fires off my function to send the webhook. It doesn't save the record. Anyone able to show me how I can include the save command in the same action?
protected function getFormActions(): array
{
return [
$this->getSaveFormAction()
->label('Save and Trigger')
->submit(null)
->action(function (MyModel $new) {
$result = $new->MyTriggerFunction('Some Data');
Notification::make()
->title($result)
->success()
->send();
}),
$this->getCancelFormAction()
];
protected function getFormActions(): array
{
return [
$this->getSaveFormAction()
->label('Save and Trigger')
->submit(null)
->action(function (MyModel $new) {
$result = $new->MyTriggerFunction('Some Data');
Notification::make()
->title($result)
->success()
->send();
}),
$this->getCancelFormAction()
];
Thanks in advance.
Solution:
Here, lifecycle hooks should be the means of handling the thing. In case of Edit Modal: https://filamentphp.com/docs/3.x/actions/prebuilt-actions/edit#lifecycle-hooks In case of Edit Page:...
Jump to solution
4 Replies
Solution
Tobias Platen
Tobias Platen12mo ago
Here, lifecycle hooks should be the means of handling the thing. In case of Edit Modal: https://filamentphp.com/docs/3.x/actions/prebuilt-actions/edit#lifecycle-hooks In case of Edit Page: https://filamentphp.com/docs/3.x/panels/resources/editing-records#lifecycle-hooks Maybe like this?
protected function afterSave(): void
{
$model = $this->getRecord();
$result = $model->MyTriggerFunction('Some Data');
Notification::make()
->title($result)
->success()
->send();
}
protected function afterSave(): void
{
$model = $this->getRecord();
$result = $model->MyTriggerFunction('Some Data');
Notification::make()
->title($result)
->success()
->send();
}
ChrisB
ChrisBOP12mo ago
Totally overlooked / misunderstood that, thank you! I've quickly tried the below, with no luck... Am I putting this in the right place within the action?
protected function getFormActions(): array
{
return [
$this->getSaveFormAction()
->label('Save and Trigger')
->after(function (MyModel $new) {
$result = $new->customFunction('Some Data');
}),
$this->getCancelFormAction()
];
}
protected function getFormActions(): array
{
return [
$this->getSaveFormAction()
->label('Save and Trigger')
->after(function (MyModel $new) {
$result = $new->customFunction('Some Data');
}),
$this->getCancelFormAction()
];
}
Tobias Platen
Tobias Platen12mo ago
Remove the after() method and insert the afterSave() method as described above.
protected function getFormActions(): array
{
return [
$this->getSaveFormAction()
->label('Save and Trigger'),
$this->getCancelFormAction()
];

protected function afterSave(): void
{
$model = $this->getRecord();
$result = $model->customFunction('Some Data');
Notification::make()
->title($result)
->success()
->send();
}
}
protected function getFormActions(): array
{
return [
$this->getSaveFormAction()
->label('Save and Trigger'),
$this->getCancelFormAction()
];

protected function afterSave(): void
{
$model = $this->getRecord();
$result = $model->customFunction('Some Data');
Notification::make()
->title($result)
->success()
->send();
}
}
ChrisB
ChrisBOP12mo ago
Nice! Thank you. Sorry, I missed the resources link in your first message, which would have detailed everything for me. Working perfectly.... Thanks again.
Want results from more Discord servers?
Add your server