F
Filament15mo ago
samee1

Action to Button component

Hi experts, I am using filament button component to display in the header. When user clicks on that button, it should open a modal window. How to trigger this action? Can I use actions? If so, how to pass data? Appreciate ideas and pointers on how to do it. By the way, I am not using resources as it is multitenancy model.
7 Replies
toeknee
toeknee15mo ago
You can use Filament Actions, you can pass in data using mountUsing() if memory serves. but what are you trying to do exactly
samee1
samee1OP15mo ago
@toeknee thanks for the reply. I am trying to add a button with pop up to show informational message. I don't understand your statement 'if memory serves'. Does mountUsing take lot of memory? After creating action, how can add the action to the page. Sorry for the newbie questions.
bwurtz999
bwurtz99915mo ago
"if memory serves" means "if he remembers correctly" - it has nothing to do with memory usage from the docs, here is an example
use App\Models\User;
use Filament\Forms;
use Filament\Pages\Actions\Action;

Action::make('updateAuthor')
->mountUsing(fn (Forms\ComponentContainer $form) => $form->fill([
'authorId' => $this->record->author->id,
]))
->action(function (array $data): void {
$this->record->author()->associate($data['authorId']);
$this->record->save();
})
->form([
Forms\Components\Select::make('authorId')
->label('Author')
->options(User::query()->pluck('name', 'id'))
->required(),
])
use App\Models\User;
use Filament\Forms;
use Filament\Pages\Actions\Action;

Action::make('updateAuthor')
->mountUsing(fn (Forms\ComponentContainer $form) => $form->fill([
'authorId' => $this->record->author->id,
]))
->action(function (array $data): void {
$this->record->author()->associate($data['authorId']);
$this->record->save();
})
->form([
Forms\Components\Select::make('authorId')
->label('Author')
->options(User::query()->pluck('name', 'id'))
->required(),
])
bwurtz999
bwurtz99915mo ago
That's a link to the v2 docs but it will still work So in the example above, authorId would get passed to the form
bwurtz999
bwurtz99915mo ago
If you are using a custom page, which it sounds like you are, you will need to follow the steps from this link: https://filamentphp.com/docs/3.x/actions/adding-an-action-to-a-livewire-component to render it on a page
samee1
samee1OP15mo ago
@bwurtz999 thanks...really helpful 🙂

Did you find this page helpful?