Jan 13, 2025
0
Level 7
Error when restoring soft deleted record
Hello. I have a model with soft deletes enabled. When using the following Filament restore table action:
RestoreAction::make()
->label(__('Restore'))
->tooltip(__('Restore task from recycle bin.'))
->requiresConfirmation()
->modalHeading(__('Restore task?'))
->modalDescription(
fn (Task $record) => __('Are you sure you\'d like to restore task :name ?', [
'name' => $record->name,
]),
)
->visible(function (Task $record): bool {
return auth()
->user()
->can('restore', $record) && $record->trashed();
})
->after(function (Task $record) use ($context, $livewireComponent) {
if ($context === 'frontend') {
$livewireComponent->dispatch(
'success-save',
type: 'success',
title: __('Task :name restored', [
'name' => $record->name,
]),
);
}
})
I get this error:
App\Livewire\Task\User\ListTasks::App\Traits\{closure}(): Argument #1 ($record) must be of type App\Models\Task, null given, called in I:\projects\prmj\vendor\filament\support\src\Concerns\EvaluatesClosures.php on line 35
for that part of the code:
fn (Task $record) => __('Are you sure you\'d like to restore task :name ?', [
'name' => $record->name,
]),
If I change the function to this:
RestoreAction::make()
->label(__('Restore'))
->tooltip(__('Restore task from recycle bin.')),
I get another error:
Cannot use "::class" on value of type null
I:\projects\prmj\vendor\filament\actions\src\Concerns\InteractsWithRecord.php :102
97 namedInjections: [
98 'record' => $record,
99 ],
100 typedInjections: [
101 Model::class => $record,
102 $record::class => $record,
103 ],
104 );
All the testing I've done makes me think that for some reason, the modal is not closing thus the errors occur. The funny part is, that I use exactly the same function for another model with soft deletes and there it works without a hitch. Any clues?
Please or to participate in this conversation.