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

goheels's avatar

Wire:model Array Shared Key?

Howdy,

Lets say I have a checkout page that allows the customer to submit multiple address's if there is more than 1 of something (qty). How can I assign a public array variable to multiple wire:model inputs? Feels like something like this should work, but it doesnt. Ive tried a lot & ran out of ideas. wire:model.lazy="address[0]" wire:model.lazy="address[1]"

public array $address = [];

Im not working with validation for testing. Everything that Ive tried returns an empty array =[ any help is greatly appreciated, ive been stuck for a couple hours far longer than I should be spending on something that seems like it should be so simple šŸ˜‚ anyhow, thanks <3

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It sounds like you're using Livewire for your checkout page and you want to bind multiple input fields to an array in your Livewire component. The syntax you've provided should work, assuming you're using it correctly within your Livewire component and your Blade template.

Here's a simple example of how you can bind multiple input fields to an array in Livewire:

First, make sure your Livewire component's class has the public array property:

use Livewire\Component;

class CheckoutComponent extends Component
{
    public $addresses = [];

    public function mount()
    {
        // Initialize the addresses array with empty values for each item
        $this->addresses = ['', '']; // Assuming you have 2 quantity for example
    }

    public function render()
    {
        return view('livewire.checkout-component');
    }
}

Next, in your Blade template, you can bind each input field to a specific index of the array:

<div>
    @foreach($addresses as $index => $address)
        <input type="text" wire:model.lazy="addresses.{{ $index }}">
    @endforeach
</div>

This will create an input field for each address in the $addresses array, and each input will be bound to the corresponding index of the array.

If you're still getting an empty array, make sure that:

  1. You are using the correct syntax in your Blade template (wire:model.lazy="addresses.{{ $index }}").
  2. You have initialized the $addresses array properly in your Livewire component.
  3. You are not resetting the $addresses array somewhere else in your component unintentionally.

If you've checked all of the above and it's still not working, try the following troubleshooting steps:

  • Ensure that you are not using wire:model and wire:model.lazy on the same input field.
  • Check for JavaScript errors in your browser's console that might be preventing Livewire from updating the property.
  • Make sure you are not using wire:key or :key incorrectly, which can sometimes cause issues with Livewire's DOM diffing.
  • If you're using nested components, ensure that the data binding is correctly set up between parent and child components.

If you continue to face issues, please provide more context or code snippets, so we can help you debug the problem further.

goheels's avatar

@tykus sheesh how embarrasing, I got it with this answer. wire:model="address.{{$i}}" works thank yuh bottt

Please or to participate in this conversation.