Lifecycle Hooks on simple resources
Where do I apply these?
In the documentation it says:
You can use the before() and after() methods to execute code before and after a record is deleted:
DeleteAction::make()
->before(function () {
// ...
})
->after(function () {
// ...
})
But where does this code go?
In the page resource?
For the edit life cycle hook it says:
"To set up a hook, create a protected method on the Edit page class with the name of the hook:"
I'm using a simple resource (modal editing only, so doesn't have an edit page?). Is this not possible, or is there another place to utilise the life cycle hooks for this case?
12 Replies
Not to sound mean or condescending but I would recommend learning how to use your IDE to inspect classes. You’ll often learn more from that than docs.
Thanks. It's interesting looking at the Action classes but I can't see any reference to lifecycle hooks in there.
I thought it was a fair question as I'm not able to decipher it from the documentation either.
"To set up a hook, create a protected method on the Edit page class with the name of the hook:"
There isn't an edit page for simple resources.
And it doesn't sound condescending - I'll take any help I can get!
search your app/Filament for DeleteAction
if you dont have it
it should be a table action on your simple resource :)
you can just add it next to the others
on a non-simple resource, its on the edit page, but you can also add it to the table if you want
Thanks. I don't have any actions in app/filament - do I need to publish them?
no, much simpler than that
have you seen how to add actions to resource table rows?
$table->actions([DeleteAction::make()])
Thanks. I did try it there with a simple example, but I get an error that I can't make sense of:
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make()
->before(function (DeleteAction $action) {
// ...
$action->halt();
})
])
App\Filament\Resources\CompanyResource::App\Filament\Resources{closure}(): Argument #1 ($action) must be of type Filament\Pages\Actions\DeleteAction, Filament\Tables\Actions\DeleteAction given
Ah I guess I need to set the ->action
you have imported the wrong DeleteAction
nope
we do it for you
you see how you are using Tables\Actions\DeleteAction and the imported DeleteAction?
you imported DeleteAction wrong
they can both be Tables\Actions\DeleteAction if you wish
Ah yes, I imported pages/DeleteAction
Thank you! It's working now.
Sorry if I'm asking too many questions on here that might seem obvious to others. I'll be less troublesome once I get up to speed.
all good
Please ask as many questions as you want. Again wasn’t trying to be mean. Just trying to offer some advice.
Thank you @awcodes ! I really appreciate the help here and the community support. 😄 And you're totally right about drilling down into classes and functions to see how they work - I certainly should do that more.
Sometimes just quick glance can save some time in having to ask and wait. But definitely don’t hesitate to ask.