Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aliefe's avatar

Livewire array property resets

In a livewire component i want to add/remove item id's(or any data) into an array. But when i dump the array i see:

  • It is every time the $ticked array is empty first and then the new value added. But previous changes does not remain.

my component class looks like this

class MyComponent extends Component
{
    public $ticked = array();
	...
	public function settick ($id)
    {   
		dump($this->ticked);
        $index = array_search($id, $this->ticked);
        if ($index !== false)
        {
            unset($this->ticked[$index]);
        }
        else
        {
            array_push($this->ticked, $id);
        }
        dump($this->ticked);
    }			
}

and this is the view part

@foreach($items as $item)
  <div>
	<p> {{ $item->name }} </p>
    <input type="checkbox" wire:click="settick({{ $item->id }})">
  <div>
@endforeach

and this is the output of dump() calls

//first dump
[]
//second dump when I click the checkbox of $item->id => 14
array:1 [▼
  0 => 14
]
// it adds 14 to the array, but there should be previous changes in the array but is empty when functions executes

sorry, this might be a dumb question but i couldn't understand why.

Update

  • Okay, the problem was dump() calls :).
  • so, now i am using
{{ count($ticked) }} 
  • for debugging (whether $ticked array changed or not).
  • It would be good if someone could explain why i shouldn't or how to use dump() in livewire method.
0 likes
4 replies
MohamedTammam's avatar

That's a basic explanation on how Livewire works.

1 - It initialize the component with the values that you get from the back-end.

2 - Whenever you do any changes on the front-end, it sends request to back-end with that changes.

3 - The back-end should return the updated component template as a response.

4 - Livewire on the front-end updated the parts that changes on the template, and only the parts that changes. That why you don't see it blinking.

Back to your question, when your are using dump or anything like it, it changes the response that Livewire expects on the front-end. So it doesn't know who to interact with it, and it stops working as it should be.

1 like

Please or to participate in this conversation.