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
awcodes
awcodes2y ago
There’s lifecycle hooks for both beforeCreate and beforeSave.
damms005
damms005OP2y ago
@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.
awcodes
awcodes2y ago
Probably $data. Not sure off the top of my head though.
damms005
damms005OP2y ago
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
awcodes
awcodes2y ago
Might have to source dive. Could probably us $this->form->getState() but that doesn’t seem right. Did you inject $data into the function?
LeandroFerreira
->before(function ($record) {
})
->before(function ($record) {
})
?
awcodes
awcodes2y ago
Yea. Could be record since it’s technically a table action.
damms005
damms005OP2y ago
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 @zepfietje
Dan Harrin
Dan Harrin2y ago
there are too many hooks, which is why the docs are with the features they relate to
damms005
damms005OP2y ago
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?
damms005
damms005OP2y ago
For reference, see this hooks without any hint about what arguments the callbacks receive:
toeknee
toeknee2y ago
Most half decent IDEs will show you in the code completion
damms005
damms005OP2y ago
VScode doesn't, and I'll believe it's one of the half decent ides out there
toeknee
toeknee2y ago
VSCode does, you just need to install intellisense and type extensions relative to the development you are doing.
LeandroFerreira
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.
damms005
damms005OP2y ago
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
damms005
damms005OP2y ago
@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.
Filament
Advanced - Form Builder - Filament
The elegant TALL stack form builder for Laravel artisans.
Andrew Wallo
Andrew Wallo2y ago
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
Want results from more Discord servers?
Add your server