Search code examples
laravellaravel-livewire

Livewire click error on wire:click='function(value)'


Help please, there is a database there are products there For example

1 Name
2 Name
3 Name 
4 Name

everyone has links <a wire:click="delete({{$item->id}})">Delete</a>

When you click on link 1, 1 is deleted, then when you click on link 2, it sends a request to the server where the value is 1, although the value is specified as 2

I tried everything and I almost can’t figure out what’s going on


Solution

  • I suppose that you use a @foreach to populate the list.

    You must add a distinct wire:key for each row that is displayed to let livewire refresh correctly the DOM:

    @foreach ($items as $item)
    
       <div wire:key="{{ 'prefix' . $item->id }}"> 
    
          <!-- ..... -->
    
          <a wire:click="delete({{$item->id}})">Delete</a>
    
       </div>
    
    @endforeach
    
    

    The 'prefix' is not mandatory but is recommended when you have multiple lists with the same ids

    Here the documentation