When using Livewire, only public properties are preserved between requests. Private properties (including private arrays) are not persisted; they get re-initialized during every request, such as when you type into a wire:model.live bound input. This is why your private arrays become empty: Livewire doesn't store or serialize private/protected properties between updates.
What happens?
- On each request, Livewire only serializes public properties.
- Private arrays are not restored; they’re re-created in the
mountmethod or via property initialization, often ending up empty if they depend on dynamic data.
How should you handle this?
- If the data does NOT need to be sent to the frontend and should just be available for internal server logic, you can use private/protected properties, but their values must be set each request (for example, in
mountand in every relevant method), not only once. - For persistent data, use a public property. You can make it protected/public and exclude it from frontend exposure using
$exceptor casting.
Example solution:
class ExampleComponent extends \Livewire\Component
{
public $inputValue;
// Only for reference, NOT persistent between requests:
private $myArray = [];
public function mount()
{
// This array will not persist between requests
$this->myArray = ['foo', 'bar'];
}
public function updated($propertyName)
{
// If you need $myArray on every request, always re-initialize here
$this->myArray = ['foo', 'bar'];
}
// OR:
// Prefer public if you need persistency,
// but then you'll have access from frontend via Livewire
public $persistentArray = ['foo', 'bar'];
}
Summary:
- Private/protected properties are NOT automatically persisted by Livewire.
- If you need the array on the backend only, re-initialize it on every request, not just in
mount. - If you want persistency, use a public property.
Further reading:
Livewire Lifecycle Hooks
Livewire Data Binding
Let me know if you need more clarification!