F
Filament16mo ago
Emaz

Looping through repeater data prior to save

In the attached video I go over the issue. An estimator uses multiple repeaters to collect inventory items, including quantity of each to total up everything for an estimate. Local totals (quantity * cost each) for each repeater element are easy enough. But how would I calculate the entire estimate since the user can go into any already existing repeater item and change the item or quantity? I think the only way is to loop through the existing repeater elements. But this is before the estimate has been created, so is this possible? Otherwise I'd have to do some complex state saving while the user interacts, which seems counter to Livewire & Filament so I must be missing something. Any insight would be appreciated.
Solution:
The repeater’s $state will be the entire array. So you can use live() and afterStateUpdated(function($state) { do something with $state }) on the Repeater::make() field.
Jump to solution
35 Replies
Emaz
EmazOP16mo ago
Am I going about this the wrong way? Surely there are lots of people doing this kind of thing for invoicing, estimates, etc. Filament seems perfectly suited for it. To elaborate ... I didn't know if there was a way to collect and total up existing repeater data since the record hasn't yet been created. So I resorted to trying to keep track of the state of everything and run my totals as you go. That turns out to be complex and error prone. That's not working. So I am back again to wondering if I can collect repeater data for totals, before we're even done creating the record.
DanielvdSpoel
DanielvdSpoel16mo ago
You can just use reactive fields right? and then just loop over the repeater values
Emaz
EmazOP16mo ago
How do I loop through the repeater values? Is there an array somewhere?
DanielvdSpoel
DanielvdSpoel16mo ago
just your normal $get parameter thingy?
Emaz
EmazOP16mo ago
I’m not in front of my code at the moment but I believe that when I am doing get in a repeater I’m only getting the current one, not an array of all of them.
Emaz
EmazOP16mo ago
Yeah, I’m using that to access parent fields. But an already filled out repeater field is a different scenario I believe. It would have to be an array somewhere I think. Good point though, I should continue to poke around with the syntax of $get.
DanielvdSpoel
DanielvdSpoel16mo ago
i really don't get what you are trying to do an already filled out repeater field doesn't work any different
Emaz
EmazOP16mo ago
Oh this might do it… $get('repeater.item1.client_id') I’ll try that, thanks.
Solution
awcodes
awcodes16mo ago
The repeater’s $state will be the entire array. So you can use live() and afterStateUpdated(function($state) { do something with $state }) on the Repeater::make() field.
Emaz
EmazOP16mo ago
Fantastic, I will try that. I’m already using afterStateUpdated. I didn’t realize that the state is going to be the entire array. Thanks!
awcodes
awcodes16mo ago
$state will always be the value of the field. In this case the value is the array.
Emaz
EmazOP16mo ago
I'm just not seeing the repeater data. The @dump never shows up at all.
Emaz
EmazOP16mo ago
Added ->live() at the repeater level and still nothing
DanielvdSpoel
DanielvdSpoel16mo ago
why the @dump? isn't it dump
Emaz
EmazOP16mo ago
I was using the Laravel directive. Usually I use @dd. And the @dump I have commented out in the select works when I uncomment. Nevertheless, same thing with just dump ... nothing.
DanielvdSpoel
DanielvdSpoel16mo ago
then idk
awcodes
awcodes16mo ago
I’m not seeing a live on the repeater.
Emaz
EmazOP16mo ago
In my screenshot, no but I added it and still nothing.
Emaz
EmazOP16mo ago
awcodes
awcodes16mo ago
There’s some issues with repeaters at the moment because of a livewire bug. Maybe that’s causing problems. Technically that should work though.
Emaz
EmazOP16mo ago
Yeah kinda feels that way. I thought that was fixed?
awcodes
awcodes16mo ago
Nope. We wish it was
Emaz
EmazOP16mo ago
Ruh roh. That'll roadblock me. Yikes.
awcodes
awcodes16mo ago
It’s an issue with any field that uses entangle. You could still do what you need in a lifecycle hook. It just wouldn’t be a real-time visual thing for the user.
Emaz
EmazOP16mo ago
😣
awcodes
awcodes16mo ago
Sorry. Kinda out of our hands and them moment.
Emaz
EmazOP16mo ago
Yeah I get it. Not your fault, obviously. But ouch. Gonna have to rethink this. Or cross my fingers that a LW fix comes fast LOL Uhhhh.... stand by. I think that bug IS fixed... just did a composer update.
awcodes
awcodes16mo ago
That would be cool. I know we’re still having issues with them, but maybe you are on a livewire beta version. LW3 was officially released out of beta yesterday.
Emaz
EmazOP16mo ago
Yep, looks like it got fixed. I can see the array now. More coding/testing until I mark as solved but yeah I think we're good.
awcodes
awcodes16mo ago
🎉 Good luck with it.
Emaz
EmazOP16mo ago
Thanks for you help! I'll be sure to mark it as solved soon. So happy for this LOL.... this was the only way to compute totals in a use case like this. Uh oh. This update broke repeaters. Only the first one works, I cannot add more. No errors either.
Emaz
EmazOP16mo ago
Looks like. a LW bug still
Emaz
EmazOP16mo ago
In case anyone else runs into this, livewire 3.0.1 fixed it for me and now @awcodes answer is correct 🥳
Want results from more Discord servers?
Add your server