Reusing Action arguments in Livewire Action

I'm using a custom edit/update action to change a category's status in Livewire, passing the category ID from the Blade file. My challenge is reusing the category instance retrieved in mountUsing() to fill the form, and again in ->action() to avoid fetching it by ID twice. I also want to avoid using public properties in Livewire.
public function updatePublishStatusAction(): Action
{
return Action::make('updatePublishStatus')
->label('Change Publish Status')
->mountUsing(function (Form $form, array $arguments) {
$category = BusinessCategory::find($arguments['categoryId']);
if ($category) {
$form->fill([
'status' => $category->status,
]);
}
})
->form([
Select::make('status')
->options(BusinessCategoryStatusEnum::class)
->default(BusinessCategoryStatusEnum::NOT_PUBLISHED),
])
->icon('heroicon-o-pencil-square')
->iconButton()
->size(ActionSize::ExtraSmall)
->action(function (array $data, array $arguments): void {
$category = BusinessCategory::find($arguments['categoryId']);
if ($category) {
$category->update([
'status' => $data['status'],
'updated_by' => auth()->id(),
'published_by' => $data['status'] === BusinessCategoryStatusEnum::PUBLISHED ? auth()->id() : null,
'published_at' => $data['status'] === BusinessCategoryStatusEnum::PUBLISHED ? now() : null,
]);
}

})
->modalHeading('Update Publish Status')
->modalSubmitActionLabel('Update Status')
->modalWidth(MaxWidth::Medium);
}
public function updatePublishStatusAction(): Action
{
return Action::make('updatePublishStatus')
->label('Change Publish Status')
->mountUsing(function (Form $form, array $arguments) {
$category = BusinessCategory::find($arguments['categoryId']);
if ($category) {
$form->fill([
'status' => $category->status,
]);
}
})
->form([
Select::make('status')
->options(BusinessCategoryStatusEnum::class)
->default(BusinessCategoryStatusEnum::NOT_PUBLISHED),
])
->icon('heroicon-o-pencil-square')
->iconButton()
->size(ActionSize::ExtraSmall)
->action(function (array $data, array $arguments): void {
$category = BusinessCategory::find($arguments['categoryId']);
if ($category) {
$category->update([
'status' => $data['status'],
'updated_by' => auth()->id(),
'published_by' => $data['status'] === BusinessCategoryStatusEnum::PUBLISHED ? auth()->id() : null,
'published_at' => $data['status'] === BusinessCategoryStatusEnum::PUBLISHED ? now() : null,
]);
}

})
->modalHeading('Update Publish Status')
->modalSubmitActionLabel('Update Status')
->modalWidth(MaxWidth::Medium);
}
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server