Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RileyGWeb's avatar

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());
            }),
    )

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]);
                            })
                    ),
  // ..

But when I click that icon, I get:

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?

0 likes
3 replies
LaryAI's avatar
Level 58

The error you're encountering, "Using $this when not in object context," typically occurs when you try to use $this in a static context. In the code snippet you provided, the issue arises because the closure you are using for the action is not bound to an object context where $this is available.

In the Filament documentation, the example assumes that the closure is being executed in a context where $this is available, such as within a component or a class method. However, in your case, the closure is being executed in a static context, which is why $this is not available.

To resolve this issue, you can use the dispatch method in a way that doesn't rely on $this. Instead, you can use the dispatch helper function if you're within a Livewire component, or you can use an event system that doesn't rely on $this. Here's how you can modify your code:

use Filament\Tables\Actions\Action;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Table;
use Livewire\Component;

class YourComponent extends Component
{
    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                IconColumn::make('playerAssignments')
                    ->icon('heroicon-o-pencil-square')
                    ->action(
                        Action::make('edit-player-assignments')
                            ->action(function (Event $record): void {
                                // Use the Livewire `dispatch` helper function
                                dispatch('open-player-assignments-modal', ['event' => $record]);
                            })
                    ),
                // ...
            ]);
    }
}

Make sure that your component extends Livewire\Component or is within a context where the dispatch helper function is available. This way, you can dispatch events without relying on $this.

If you're not using Livewire or if the dispatch helper is not available, you might need to implement a custom event system or use a different approach to handle the action.

RileyGWeb's avatar

@LaryAI Nice try Lary, but dispatch as a helper function doesn't exist (to my knowledge). I tried that and it gave me:

class_parents(): Class open-player-assignments-modal does not exist and could not be loaded

I'm also not in a Livewire\Component scope, I'm in a filament resource. So I wouldn't expect that to work even if it did exist (does it? still not sure).

RileyGWeb's avatar

The nice grownups on Discord gave me a solution, and it's to pass in the $livewire component object, which needs to be an instance of your resource's list page:

IconColumn::make('playerAssignments')
    ->icon('heroicon-o-pencil-square')
    ->action(
        \Filament\Tables\Actions\Action::make('edit-player-assignments')
            ->action(function (Event $record, \App\Filament\Resources\[name]Resource\Pages\List[name] $livewire): void {
                $livewire->dispatch('open-player-assignments-modal', ['event' => $record]);
            })
    ),

Please or to participate in this conversation.