mrweb's avatar
Level 1

Livewire: emitting/updating data in a foreach loop

I have a Livewire component which uploads one or more pictures. I have a foreach loop

foreach ($this->uploads as $upload) {
//Upload stuff
}

I want to show the current status of upload, so I thought I had to update a property like so:

public $current = 0;

...

foreach ($this->uploads as $upload) {
//Upload stuff
$this->current++;
}

But this is not updating the model until the foreach finishes and at the end I have a final $current with the total of uploaded images.

So I thought I would emit an event to a "progress-bar" component like so:

foreach ($this->uploads as $upload) {
//Upload stuff
$this->current += 1;
$this->emit('updateProgressBar', $current);
}

But this gives the same result.... How can I emit an event or update a property after each image uploaded?

0 likes
1 reply

Please or to participate in this conversation.