Using refreshFormData() in static form methods

Hello everyone, I am a little confused by the "Refreshing form data" part of the documentation. It states that if "you're using actions on an Edit or View resource page, you can refresh data within the main form" which is exactly what I want. The example provided is:
Action::make('approve')
->action(function (Post $record) {
$record->approve();

$this->refreshFormData([
'status',
]);
})
Action::make('approve')
->action(function (Post $record) {
$record->approve();

$this->refreshFormData([
'status',
]);
})
The thing is that a resource page's form method is static, and therefore it is not possible to call $this->refreshFormData() from within the action method. The obvious error (if someone searches by keyword):
Using $this when not in object context
Using $this when not in object context
What am I missing here?
class DocumentResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([
Section::make('AI generated content')
->headerActions([
Action::make('Auto-generate fields')
->icon('heroicon-m-sparkles')
->action(function (Document $record) {
UpdateDocumentMetadata::run($record);
$form->refreshFormData();
})
])
->schema([
// ...
class DocumentResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema([
Section::make('AI generated content')
->headerActions([
Action::make('Auto-generate fields')
->icon('heroicon-m-sparkles')
->action(function (Document $record) {
UpdateDocumentMetadata::run($record);
$form->refreshFormData();
})
])
->schema([
// ...
I suppose there is a way to inject the property into the function?
Solution:
In this context, iirc, you can inject $livewire into the ->action() callback then call it off of the $livewire object.
Jump to solution
3 Replies
Grégoire
GrégoireOP6mo ago
One thing that I ended up doing and works is using the Set class to set the value of the field. Not sure if this is the best way to do it, but it works.
Action::make('Auto-generate fields')
->icon('heroicon-m-sparkles')
->action(function (Document $record, Set $set) {
$document = UpdateDocumentMetadata::run($record);
$set('summary', $document->summary);
})
Action::make('Auto-generate fields')
->icon('heroicon-m-sparkles')
->action(function (Document $record, Set $set) {
$document = UpdateDocumentMetadata::run($record);
$set('summary', $document->summary);
})
Solution
awcodes
awcodes6mo ago
In this context, iirc, you can inject $livewire into the ->action() callback then call it off of the $livewire object.
Grégoire
GrégoireOP6mo ago
I checked and indeed it seems to be possible, thanks @awcodes 🙂
Action::make('Auto-generate fields')
->icon('heroicon-m-sparkles')
->action(function (Document $record, Component $component) {
$document = UpdateDocumentMetadata::run($record);
$livewire->refreshFormData(['summary']);
})
Action::make('Auto-generate fields')
->icon('heroicon-m-sparkles')
->action(function (Document $record, Component $component) {
$document = UpdateDocumentMetadata::run($record);
$livewire->refreshFormData(['summary']);
})
Want results from more Discord servers?
Add your server