F
Filament4mo ago
Sayy

how to custom actions on create record

how custom actions if the actions on create record, because im confuse after click the button actions then can generate pdf or something update data from im input on view create ?
protected function getFormActions(): array
{
return [
Actions\CreateAction::make()->label('Simpan & Cetak')
->color('warning')
->using(function (array $data): Model {
return static::getModel()::create($data);
})->action(function (Order $record) {
$pdf = Pdf::loadView('pdf.print-order', [
'order' => $record,
]);

return response()->streamDownload(function () use ($pdf) {
echo $pdf->stream();
}, 'receipt-' . $record->order_number . '.pdf');
})->submit('form')
->button(),
...parent::getFormActions(),
];
}
protected function getFormActions(): array
{
return [
Actions\CreateAction::make()->label('Simpan & Cetak')
->color('warning')
->using(function (array $data): Model {
return static::getModel()::create($data);
})->action(function (Order $record) {
$pdf = Pdf::loadView('pdf.print-order', [
'order' => $record,
]);

return response()->streamDownload(function () use ($pdf) {
echo $pdf->stream();
}, 'receipt-' . $record->order_number . '.pdf');
})->submit('form')
->button(),
...parent::getFormActions(),
];
}
No description
Solution:
probably your repeater has a relationship.. if you want to access the repeater data in $data, you need to add ->dehydrated() in the repeater
Jump to solution
18 Replies
Sayy
SayyOP4mo ago
functional its same like create, but i just want to update my data if i click this button, or just print my pdf from $this->record how to get $this->record with custom ?
LeandroFerreira
LeandroFerreira4mo ago
$this->record isn't available in the create action... I think you can use $get $set or $this->data to access the form data
Sayy
SayyOP4mo ago
then how if the data its same like create function but just want to update data ?
Actions\CreateAction::make()
->label('Simpan Draft')
->action(function (){

}),
Actions\CreateAction::make()
->label('Simpan Draft')
->action(function (){

}),
like this on function ? can give me example sir ?
LeandroFerreira
LeandroFerreira4mo ago
->action(function (array $data){... ?
Sayy
SayyOP4mo ago
depends after click button will save ? like same with default create? because this action in form action
Sayy
SayyOP4mo ago
like this form but i just custom actions then i will get the data from this after fill, then generate pdf for print
No description
LeandroFerreira
LeandroFerreira4mo ago
this will save if you call $this->create() method
Sayy
SayyOP4mo ago
inside action ?
LeandroFerreira
LeandroFerreira4mo ago
yes, the default CreateAction calls create()
Sayy
SayyOP4mo ago
ok but how to get data form then i want to generate pdf to print with this code like this maybe ?
Actions\CreateAction::make()->label('Simpan & Cetak')
->color('warning')
->action(function (Order $record) {
$pdf = Pdf::loadView('pdf.print-order', [
'order' => $record,
]);

return response()->streamDownload(function () use ($pdf) {
echo $pdf->stream();
}, 'receipt-' . $record->order_number . '.pdf');
$this->create();
})
Actions\CreateAction::make()->label('Simpan & Cetak')
->color('warning')
->action(function (Order $record) {
$pdf = Pdf::loadView('pdf.print-order', [
'order' => $record,
]);

return response()->streamDownload(function () use ($pdf) {
echo $pdf->stream();
}, 'receipt-' . $record->order_number . '.pdf');
$this->create();
})
LeandroFerreira
LeandroFerreira4mo ago
protected function getFormActions(): array
{
return [
...parent::getFormActions(),
Action::make('customAction')
->action(function () {
$data = $this->form->getState();
//...
}),
];
}
protected function getFormActions(): array
{
return [
...parent::getFormActions(),
Action::make('customAction')
->action(function () {
$data = $this->form->getState();
//...
}),
];
}
$data is an array with the form values..
Sayy
SayyOP4mo ago
if click this will open modal right ?
LeandroFerreira
LeandroFerreira4mo ago
this example is using form actions in your CreatePage
Sayy
SayyOP4mo ago
then how to access the data form $this->form->getState()
LeandroFerreira
LeandroFerreira4mo ago
how? $data['your_field'] ? check
->action(function () {
$data = $this->form->getState();
dd($data);
}),
->action(function () {
$data = $this->form->getState();
dd($data);
}),
Sayy
SayyOP4mo ago
yeah i think can already check. but i have data repeater then how to get this ? because already check my repeater not show
Solution
LeandroFerreira
LeandroFerreira4mo ago
probably your repeater has a relationship.. if you want to access the repeater data in $data, you need to add ->dehydrated() in the repeater
Sayy
SayyOP4mo ago
ok i will try it wait if the $this->form->getState() with create first then get data its possible ? thanks for help me sir, already clear
Want results from more Discord servers?
Add your server