F
Filamentβ€’2mo ago
Asmit

Help: Filament Action Record Context Issue - Custom Implementation

Hey everyone! I'm working with Filament actions in a custom context and running into an issue with record context. The Problem: In my blade template, when I dd($action) it shows the record is attached βœ… But when the action executes (user clicks), $record is null in the callback ❌
// Action definition - record is null here when executed
Action::make('update')
->action(fn ($record) => dd($record)) // null

// But this works fine:
Action::make('update')
->record(fn() => Document::find(1))
->action(fn ($record) => dd($record)) // shows record
// Action definition - record is null here when executed
Action::make('update')
->action(fn ($record) => dd($record)) // null

// But this works fine:
Action::make('update')
->record(fn() => Document::find(1))
->action(fn ($record) => dd($record)) // shows record
Blade:
@foreach($actions as $action)
{{ $action->record($record) }} {{-- Record shows here in dd() --}}
@endforeach
@foreach($actions as $action)
{{ $action->record($record) }} {{-- Record shows here in dd() --}}
@endforeach
What works: 1. Native Filament table actions automatically get the record 2. Setting record explicitly with closure in action definition Question: How does Filament's native table actions automatically inject the record during execution? Any insights on replicating Filament's internal record injection mechanism for custom action implementations? Thanks! πŸ™
5 Replies
Asmit
AsmitOPβ€’2mo ago
Any one have any Idea about it, Please suggest some approach
kschwab
kschwabβ€’2mo ago
I have a similar issue and came up with an ugly workaround. I imagine there is a better solution, but I don't know it. Something like this might work for you:
Action::make('update')
->model(MyModel::class)
->before(function(Action $action, array $arguments) {
$action->record(MyModel::find($arguments['my_model_id']));
})
->beforeFormFilled(function(Action $action, array $arguments) {
$action->record(MyModel::find($arguments['my_model_id']));
})
Action::make('update')
->model(MyModel::class)
->before(function(Action $action, array $arguments) {
$action->record(MyModel::find($arguments['my_model_id']));
})
->beforeFormFilled(function(Action $action, array $arguments) {
$action->record(MyModel::find($arguments['my_model_id']));
})
@foreach($actions as $action)
{{ ($action)->(['my_model_id' => $record->id]) }}
@endforeach
@foreach($actions as $action)
{{ ($action)->(['my_model_id' => $record->id]) }}
@endforeach
Asmit
AsmitOPβ€’2mo ago
@kschwab Thanks for the reply, I tried this approach as well, but it still triggers a fresh query on every action.
kschwab
kschwabβ€’2mo ago
Unless I'm mistaken, I think that's what the native Filament tables do as well. Diving into the source, it looks like Filament queries for the record in the
resolveTableRecord
resolveTableRecord
method in
vendor/filament/tables/src/Concerns/HasRecords.php
vendor/filament/tables/src/Concerns/HasRecords.php
Asmit
AsmitOPβ€’2mo ago
You are right @kschwab thanks!

Did you find this page helpful?