F
Filamentβ€’2y ago
at92

Unable to dispatch/listen event from Action

I'm unable to dispatch and/or listen to a Livewire event from an Action. I have a resource with a table and a custom action that looks like this:
Action::make('switchTeam')
->label(__('Switch team'))
->icon('heroicon-s-arrow-right-circle')
->color('gray')
->action(function (Team $team) {
// Something happens here
})
->dispatch( 'switchedTeam' )
Action::make('switchTeam')
->label(__('Switch team'))
->icon('heroicon-s-arrow-right-circle')
->color('gray')
->action(function (Team $team) {
// Something happens here
})
->dispatch( 'switchedTeam' )
I then have a custom livewire component that I've loaded through render hooks inside the header bar next to the user's menu. In the component I've tried the following methods but nothing is being fired. 1st test:
protected $listeners = [
'switchedTeam' => 'fireEvent',
];
protected $listeners = [
'switchedTeam' => 'fireEvent',
];
2nd test:
#[On('switchedTeam')]
public function fireEvent() {
Log::info('fireEvent');
}
#[On('switchedTeam')]
public function fireEvent() {
Log::info('fireEvent');
}
But nothing is happening. What am I doing wrong? Thanks πŸ™
Solution:
try this: ```php ->action(function (Team $team) { //action here })...
Jump to solution
2 Replies
Solution
LeandroFerreira
LeandroFerreiraβ€’2y ago
try this:
->action(function (Team $team) {
//action here
})
->after(fn ($livewire) => $livewire->dispatch('switchedTeam'))
->action(function (Team $team) {
//action here
})
->after(fn ($livewire) => $livewire->dispatch('switchedTeam'))
your component
use Livewire\Attributes\On;

#[On('switchedTeam')]
public function fireEvent()
{
Log::info('fireEvent');
}
use Livewire\Attributes\On;

#[On('switchedTeam')]
public function fireEvent()
{
Log::info('fireEvent');
}
at92
at92OPβ€’2y ago
Thanks a lot that worked πŸ™

Did you find this page helpful?