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:Jump to 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.35 Replies
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.
You can just use reactive fields right?
and then just loop over the repeater values
How do I loop through the repeater values? Is there an array somewhere?
just your normal $get parameter thingy?
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.
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.
i really don't get what you are trying to do
an already filled out repeater field doesn't work any different
Oh this might do it…
$get('repeater.item1.client_id')
I’ll try that, thanks.
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.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!
$state will always be the value of the field. In this case the value is the array.
I'm just not seeing the repeater data. The @dump never shows up at all.
Added ->live() at the repeater level and still nothing
why the @dump?
isn't it dump
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.
then idk
I’m not seeing a live on the repeater.
In my screenshot, no but I added it and still nothing.
There’s some issues with repeaters at the moment because of a livewire bug. Maybe that’s causing problems.
Technically that should work though.
Yeah kinda feels that way. I thought that was fixed?
Nope.
We wish it was
Ruh roh. That'll roadblock me. Yikes.
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.
😣
Sorry.
Kinda out of our hands and them moment.
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.
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.
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.
🎉
Good luck with it.
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.
Looks like. a LW bug still
In case anyone else runs into this, livewire 3.0.1 fixed it for me and now @awcodes answer is correct 🥳