F
Filament2mo ago
dyo

Before method in repeater delete action

How can I have like a before method in repeater delete action?
14 Replies
igorclauss
igorclauss2mo ago
You can call lifecycle hook methods on the action. Why can't you use the ->after() hook?
dyo
dyoOP2mo ago
nope, it doesnt work in repeater class can someone help me?
toeknee
toeknee2mo ago
I don't think there is such an option, what are you trying to acheive?
toeknee
toeknee2mo ago
mutate options are here: https://filamentphp.com/docs/3.x/forms/fields/repeater You could use a custom deleteAction to preform actions before etc.
dyo
dyoOP2mo ago
I want to calculate the data in other table before or after the repeater record is deleted. How can I use deleteAction?
toeknee
toeknee2mo ago
Ahh so just add the state to the repeater ->afterStateUpdated()
dyo
dyoOP2mo ago
I tried with ->afterStateUpdated(fn($state) => dd($state)) But the return is the remaining item, not the deleted item. I need the deleted item id..
awcodes
awcodes2mo ago
What do you mean by “other table”?
dyo
dyoOP2mo ago
for example, I have to update the qty on table stock if one of the goods is deleted in repeater..
awcodes
awcodes2mo ago
I just don’t understand a separate table and form in this context. Like a table reads from the db via an eloquent query, so how can the table be updated until after the form is submitted and persisted. I certainly could be misunderstanding though.
dyo
dyoOP2mo ago
here's the simplified schema of my case. distribution table: id, company distribution_detail table: distribution_id, goods_id, qty stock table: goods_id, company_id, qty goods: id, name the repeater is the distribution_detail at the distribution form.
awcodes
awcodes2mo ago
Is the repeater not tied to the stock relationship? Even if it is, deleting an item from the repeater won’t affect the stock until the form is saved. Which makes sense because you don’t want to affect the stock until the action is persisted.
dyo
dyoOP2mo ago
God bless, I ended solved it with observer.. thanks for your replies before @awcodes and @toeknee
Hung Thai
Hung Thai2mo ago
You need to create a custom delete action and disable the origin delete action from the repeater. like Repeater::make()...->deletable(false)->extraItemActions([ ... //your custom delete logic here ]) you can delete the Item from repeater like:
...->action(function (array $arguments, Repeater $component): void {
$items = $component->getState();
unset($items[$arguments['item']]);
$component->state($items);
$component->callAfterStateUpdated();
})
...->action(function (array $arguments, Repeater $component): void {
$items = $component->getState();
unset($items[$arguments['item']]);
$component->state($items);
$component->callAfterStateUpdated();
})
Want results from more Discord servers?
Add your server