Running action throws "using $this when not in object context"

I'm working in the table of a standard Filament resource. The documentation says I can do this: https://filamentphp.com/docs/3.x/tables/columns/getting-started#action-modals
use Filament\Tables\Actions\Action;
use Filament\Tables\Columns\TextColumn;

TextColumn::make('title')
->action(
Action::make('select')
->requiresConfirmation()
->action(function (Post $record): void {
$this->dispatch('select-post', post: $record->getKey());
}),
)
use Filament\Tables\Actions\Action;
use Filament\Tables\Columns\TextColumn;

TextColumn::make('title')
->action(
Action::make('select')
->requiresConfirmation()
->action(function (Post $record): void {
$this->dispatch('select-post', post: $record->getKey());
}),
)
So I did:
public static function table(Table $table): Table
{
return $table
->columns([
IconColumn::make('playerAssignments')
->icon('heroicon-o-pencil-square')
->action(
\Filament\Tables\Actions\Action::make('edit-player-assignments')
->action(function (Event $record): void {
$this->dispatch('open-player-assignments-modal', ['event' => $record]);
})
),
// ..
public static function table(Table $table): Table
{
return $table
->columns([
IconColumn::make('playerAssignments')
->icon('heroicon-o-pencil-square')
->action(
\Filament\Tables\Actions\Action::make('edit-player-assignments')
->action(function (Event $record): void {
$this->dispatch('open-player-assignments-modal', ['event' => $record]);
})
),
// ..
But when I click that icon, I get:
Using $this when not in object context

$this->dispatch('open-player-assignments-modal', ['event' => $record]);
Using $this when not in object context

$this->dispatch('open-player-assignments-modal', ['event' => $record]);
Seems odd they would put something in the docs that doesn't work, but maybe it's not intended for regular resource tables? I can't remove static from the table function here. I wouldn't even mind that the docs gave me erroneous code, if I could find a workaround, which I can't. What should I do?
Solution:
->action(function (Post $record, Page $livewire)... then, use $livewire->dispatch(.....
Jump to solution
1 Reply
Solution
LeandroFerreira
->action(function (Post $record, Page $livewire)... then, use $livewire->dispatch(..

Did you find this page helpful?