F
Filament4w ago
core

Reordering and Observers

what's the trick to triggerng an observer from a filament table : ->reorderable('sort'); ? I need to clear cache on reordering items. I set an Observer but the updating of sort records is not triggering the models observer
Solution:
Instead of triggerring an event, override the method returning the parent then handle your after event changes i.e. https://github.com/filamentphp/filament/discussions/6619...
GitHub
Event or Closure after reordering table? · filamentphp filament · D...
Perchance, is it possible to set up an event after reordering a table with ->reorderable()? Or maybe pass a Closure that in some way is executed after reordering?
Jump to solution
7 Replies
toeknee
toeknee4w ago
Observers wouldn't be triggerred from a table, they are triggered by model events. So we would load each and update the record and if the record is updated i.e. (Saved) it should trigger the observer
core
coreOP4w ago
but the reorderable() is updating the model. or am I missing something?
toeknee
toeknee4w ago
If you have clicked save then yet the observer should be triggered. Is the observer triggered for create/edit?
core
coreOP4w ago
yes it is in the edit page . but on the table the sort field is updated on all records the ordering so there is an update event
toeknee
toeknee4w ago
The issue looks to be we are not handling it per model for performance we do a single query update. i.e. update contacts set order = case when id = '2' then 1 when id = '1' then 2 when id = '4' then 3 when id = '6' then 4 when id = '8' then 5 when id = '10' then 6 when id = '5' then 7 when id = '19' then 8 end, contacts.updated_at = '2025-02-06 10:42:58' where id in ('2', '1', '4', '6', '8', '10', '5', '19') Which would mean a model update isn't triggered. Let me see if we have an event
Solution
toeknee
toeknee4w ago
Instead of triggerring an event, override the method returning the parent then handle your after event changes i.e. https://github.com/filamentphp/filament/discussions/6619
GitHub
Event or Closure after reordering table? · filamentphp filament · D...
Perchance, is it possible to set up an event after reordering a table with ->reorderable()? Or maybe pass a Closure that in some way is executed after reordering?
core
coreOP4w ago
I managed to use this in the ListResource.
public function reorderTable(array $order): void
{
parent::reorderTable($order);

Log::info('Table reordered, clearing cache');
public function reorderTable(array $order): void
{
parent::reorderTable($order);

Log::info('Table reordered, clearing cache');

Did you find this page helpful?