How can I get changes to resource form?
I want to log changes to a pivot table. Creating a pivot Model is not an option, so I need to hook to the before-save event, so I can get the old/new values to log.
How can I achieve this please?
18 Replies
There’s lifecycle hooks for both beforeCreate and beforeSave.
@awcodes It's in a relation manager. So I am using the before hook instead. https://filamentphp.com/docs/2.x/admin/resources/relation-managers#lifecycle-hooks-1
However, the arguments passed to the hooks callbacks are not documented. Please how can I read the before/after state of the edited data?
PS: I want to log when a user's permissions is added/edited. I am using Spatie's activity log package. The permission are edited in a
App\Filament\Resources\StaffResource\RelationManagers\UserModelRelationManager
which is a relation to the corresponding App\Filament\Resources\StaffResource
Filament
Relation managers - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
Probably $data. Not sure off the top of my head though.
Got nothing. I dd() $data. Can you point me to the relevant part of the docs. I checked places I expected to find such details (mostly where those lifecylce hooks are documented) but got nothing
Might have to source dive. Could probably us $this->form->getState() but that doesn’t seem right.
Did you inject $data into the function?
?
Yea. Could be record since it’s technically a table action.
Thanks @awcodes @Leandro Ferreira . I was able to use
$record->getOriginal()
etc. to compute the permission changes for my logging requirements. Thanks a lot.
PS: I wish v3's doc will come with hooks and lifecylce events documentation separately (like how Testing
currently has its own dedicated documentation page). This can help in cases like this. I can contribute this if required. cc: @Dan Harrin @zepfietjethere are too many hooks, which is why the docs are with the features they relate to
But then it will be easy to document the arguments in one place. You saw how @awcodes had to guess the arguments to the callback before @Leandro Ferreira helped out. According to him, he'll have had to source-dive to figure that out. At least that part of arguments to docs should be fixed. No?
For reference, see this hooks without any hint about what arguments the callbacks receive:
Most half decent IDEs will show you in the code completion
VScode doesn't, and I'll believe it's one of the half decent ides out there
VSCode does, you just need to install intellisense and type extensions relative to the development you are doing.
Don't get me wrong, but I really appreciate that Filament is completely free, and whenever I encounter a challenge, I make an effort to dig into the code and find a way to solve it.
True, extensions can help. But you need to understand how callbacks work to understand why IDEs cannot help in this instance. Think of first-class callables
I understand it is in appreciation of the team's effort that make you source-dive when necessary. However, you should understand that it is in the same spirit that I volunteered above to contribute to such documentation should my suggestion be found needful
@Dan Harrin One other suggestion is to update closure customization to sections (https://filamentphp.com/docs/2.x/forms/advanced#using-closure-customization), such that relevant sections can be linked in the hooks/lifecylce docs where they are useful to. Again, I can contribute this if found needful.
Tbh the best documentation for me other than Filament docs of course has just been looking at the source code of Filament plugins... Maybe it's not what you prefer, but 99% of all use cases can be found by looking through them